<!DOCTYPE html>


<html lang="zh_CN">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>SpringBoot |  MyBlog</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="MyBlog" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-SpringBoot笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  SpringBoot
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/12/10/SpringBoot%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-12-09T16:00:00.000Z" itemprop="datePublished">2021-12-10</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">26.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">126 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>[TOC]</p>
<h1 id="一、SpringBoot基础入门"><a href="#一、SpringBoot基础入门" class="headerlink" title="一、SpringBoot基础入门"></a>一、SpringBoot基础入门</h1><blockquote>
<p>SpringBoot四大核心</p>
</blockquote>
<p><strong>自动配置</strong></p>
<p><strong>起步依赖</strong></p>
<p><strong>Actuator</strong></p>
<p><strong>命令行界面</strong></p>
<h2 id="1、Spring与SpringBoot"><a href="#1、Spring与SpringBoot" class="headerlink" title="1、Spring与SpringBoot"></a>1、Spring与SpringBoot</h2><h3 id="1-1Spring的能力"><a href="#1-1Spring的能力" class="headerlink" title="1.1Spring的能力"></a>1.1Spring的能力</h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112102008027.png" alt="image-20211210200850106"></p>
<h3 id="1-2Spring的生态"><a href="#1-2Spring的生态" class="headerlink" title="1.2Spring的生态"></a>1.2Spring的生态</h3><p><a target="_blank" rel="noopener" href="https://spring.io/projects/spring-boot">https://spring.io/projects/spring-boot</a></p>
<p>覆盖了：</p>
<p>web开发</p>
<p>数据访问</p>
<p>安全控制</p>
<p>分布式</p>
<p>消息服务</p>
<p>移动开发</p>
<p>批处理</p>
<p>….</p>
<h3 id="1-3-SpringBoot缺点"><a href="#1-3-SpringBoot缺点" class="headerlink" title="1.3 SpringBoot缺点"></a>1.3 SpringBoot缺点</h3><ul>
<li>Create stand-alone Spring applications<ul>
<li>创建独立Spring应用</li>
</ul>
</li>
<li>Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)<ul>
<li>内嵌web服务器</li>
</ul>
</li>
<li>Provide opinionated ‘starter’ dependencies to simplify your build configuration<ul>
<li>自动starter依赖，简化构建配置</li>
</ul>
</li>
<li>Automatically configure Spring and 3rd party libraries whenever possible<ul>
<li>自动配置Spring以及第三方功能</li>
</ul>
</li>
<li>Provide production-ready features such as metrics, health checks, and externalized configuration<ul>
<li>提供生产级别的监控、健康检查及外部化配置</li>
</ul>
</li>
<li>Absolutely no code generation and no requirement for XML configuration<ul>
<li>无代码生成、无需编写XML</li>
</ul>
</li>
</ul>
<h3 id="1-4SpringBoot缺点"><a href="#1-4SpringBoot缺点" class="headerlink" title="1.4SpringBoot缺点"></a>1.4SpringBoot缺点</h3><ul>
<li>人称版本帝，迭代快，需要时刻关注变化</li>
<li>封装太深，内部原理复杂，不容易精通</li>
</ul>
<h2 id="2-SpringBoot入门"><a href="#2-SpringBoot入门" class="headerlink" title="2.SpringBoot入门"></a>2.SpringBoot入门</h2><p>1.系统要求</p>
<ul>
<li>Java 8 &amp; 兼容java14</li>
<li>Maven 3.3+</li>
<li>idea 2019.1.2</li>
</ul>
<p>maven设置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mirrors</span>&gt;</span></span><br><span class="line">	  <span class="comment">&lt;!-- 阿里云仓库 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mirror</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">id</span>&gt;</span>nexus-aliyun<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">name</span>&gt;</span>Nexus aliyun<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">url</span>&gt;</span>https://maven.aliyun.com/nexus/content/groups/public<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></span><br><span class="line">       </span><br><span class="line">  <span class="tag">&lt;/<span class="name">mirrors</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>2.<strong>创建一个Module，选择类型为Spring Initializr快速构建</strong></p>
<p>3.项目结构：</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202212118225.png" alt="image-20220221211826522"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">主程序类</span><br><span class="line">@SpringBootApplication:SpringBoot的核心，只要用于开启Spring自动配置</span><br><span class="line"></span><br><span class="line">static：存放静态资源，如图片、CSS、JavaScript 等</span><br><span class="line"></span><br><span class="line">templates：存放 Web 页面的模板文件</span><br><span class="line"></span><br><span class="line">application.properties/application.yml 用于存放程序的各种依赖模块的配置信息，比如 服务</span><br><span class="line"></span><br><span class="line">端口，数据库连接配置等</span><br></pre></td></tr></table></figure>



<h2 id="3-了解SpringBoot的自动配置原理"><a href="#3-了解SpringBoot的自动配置原理" class="headerlink" title="3.了解SpringBoot的自动配置原理"></a>3.了解SpringBoot的自动配置原理</h2><h3 id="1-SpringBoot的特点"><a href="#1-SpringBoot的特点" class="headerlink" title="1.SpringBoot的特点"></a>1.SpringBoot的特点</h3><h4 id="1-1依赖管理"><a href="#1-1依赖管理" class="headerlink" title="1.1依赖管理"></a>1.1依赖管理</h4><ul>
<li>依赖管理</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">依赖管理    </span><br><span class="line"><span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.4.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line"></span><br><span class="line">他的父项目</span><br><span class="line"> <span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.4.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line"></span><br><span class="line">在spring-boot-dependencies中几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制</span><br></pre></td></tr></table></figure>

<ul>
<li>开发导入starter场景启动器</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">1、见到很多 spring-boot-starter-* ： *就某种场景</span><br><span class="line">2、只要引入starter，这个场景的所有常规需要的依赖我们都自动引入</span><br><span class="line">3、SpringBoot所有支持的场景</span><br><span class="line">https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter</span><br><span class="line">4、见到的  *-spring-boot-starter： 第三方为我们提供的简化开发的场景启动器。</span><br><span class="line">5、所有场景启动器最底层的依赖</span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.4.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">scope</span>&gt;</span>compile<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>无需关注版本号，自动仲裁</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1、引入依赖默认都可以不写版本</span><br><span class="line">2、引入非版本仲裁的jar，要写版本号。</span><br></pre></td></tr></table></figure>

<ul>
<li>可以修改版本号</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。</span><br><span class="line">2、在当前项目里面重写配置</span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mysql.version</span>&gt;</span>5.1.43<span class="tag">&lt;/<span class="name">mysql.version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2自动配置"><a href="#1-2自动配置" class="headerlink" title="1.2自动配置"></a>1.2自动配置</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SpringBoot框架web项目的起步依赖，它--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202212145879.png" alt="image-20220221214536161"></p>
<ul>
<li>引入SpringMVC全套组件</li>
<li>自动配好SpringMVC常用组件（功能）</li>
<li>自动配好Web常见功能，如：字符编码问题</li>
<li>SpringBoot帮我们配置好了所有web开发的常见场景</li>
<li>默认的包结构</li>
<li>主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来</li>
<li>无需以前的包扫描配置——&gt;&lt;context:component-scan base-package…./&gt;</li>
<li>想要改变扫描路径，@SpringBootApplication(scanBasePackages=<strong>“com.atyuan”</strong>) 或者@ComponentScan 指定扫描路径</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line">等同于</span><br><span class="line"><span class="meta">@SpringBootConfiguration</span></span><br><span class="line"><span class="meta">@EnableAutoConfiguration</span></span><br><span class="line"><span class="meta">@ComponentScan(&quot;com.atyuan.boot&quot;)</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>各种配置拥有默认值</p>
</li>
<li><ul>
<li>默认配置最终都是映射到某个类上，如：MultipartProperties</li>
<li>配置文件的值最终会绑定每个类上，这个类会在容器中创建对象</li>
</ul>
</li>
<li><p>按需加载所有自动配置项</p>
</li>
<li><ul>
<li>非常多的starter</li>
<li>引入了哪些场景这个场景的自动配置才会开启</li>
</ul>
</li>
<li><ul>
<li>SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面</li>
<li></li>
</ul>
</li>
<li><p>……</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration(proxyBeanMethod=true)</span>  <span class="comment">//外部无论对配置类中的这个组件注册方法调用多少次，获取到的都是之前注册到容器的单实例(全模式Full)</span></span><br></pre></td></tr></table></figure>

<p><strong>如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。</strong></p>
<p>@Import导入组件</p>
<p>用法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Import(&#123;User.class,Pet.class&#125;)</span>  <span class="comment">//默认组件的名字是全限定类名</span></span><br></pre></td></tr></table></figure>

<p>@Conditional</p>
<p>条件装配：满足Conditional指定的条件，则进行组件注入</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112110005222.png" alt="image-20211211000520127"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ConditionOnBean</span>当容器中有某组件时，才能生效</span><br><span class="line"><span class="meta">@ImportResource(classpath:配置文件)</span>  ：将配置文件导入容器中</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">只有在容器中的组件，才有SpringBoot提供的强大功能</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="meta">@ConfigurationProperties(prefix = &quot;mycar&quot;)</span></span><br><span class="line"><span class="comment">//在配置文件中，只需要使用mycar.就可以显示可以配置的相关属性</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>或者</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@EnableConfigurationProperties(Car.class)</span>  <span class="comment">//开启Car属性配置功能,并注册到容器中</span></span><br></pre></td></tr></table></figure>

<h3 id="2-自动配置原理"><a href="#2-自动配置原理" class="headerlink" title="2.自动配置原理"></a>2.自动配置原理</h3><p>2.1 引导加载自动配置类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootConfiguration</span></span><br><span class="line"><span class="meta">@EnableAutoConfiguration</span></span><br><span class="line"><span class="meta">@ComponentScan(excludeFilters = &#123; @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),</span></span><br><span class="line"><span class="meta">		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) &#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> SpringBootApplication &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>@SpringBootConfiguration</p>
</blockquote>
<p>代表当前是一个配置类</p>
<blockquote>
<p>@ComponentScan</p>
</blockquote>
<p>指定扫描哪些包</p>
<blockquote>
<p>@EnableAutoConfiguration</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AutoConfigurationPackage</span></span><br><span class="line"><span class="meta">@Import(AutoConfigurationImportSelector.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> EnableAutoConfiguration &#123;</span><br></pre></td></tr></table></figure>

<p>1.@AutoConfigurationPackage</p>
<p>自动配置包：指定默认的包规则</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Import(AutoConfigurationPackages.Registrar.class)</span>  <span class="comment">//给容器中导入一个组件</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> AutoConfigurationPackage &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//利用Registrar给容器中导入一系列组件</span></span><br><span class="line"><span class="comment">//将指定的一个包下的所有组件导入进来？MainApplication 所在包下。</span></span><br></pre></td></tr></table></figure>

<p>2.@Import(AutoConfigurationImportSelector.class)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件</span><br><span class="line"><span class="number">2</span>、调用List&lt;String&gt; configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类</span><br><span class="line"><span class="number">3</span>、利用工厂加载 Map&lt;String, List&lt;String&gt;&gt; loadSpringFactories(<span class="meta">@Nullable</span> ClassLoader classLoader)；得到所有的组件</span><br><span class="line"><span class="number">4</span>、从META-INF/spring.factories位置来加载一个文件。</span><br><span class="line">	默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件</span><br><span class="line">    spring-boot-autoconfigure-<span class="number">2.5</span><span class="number">.5</span>.jar包里面也有META-INF/spring.factories</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112111322552.png" alt="image-20211211132227560"></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112111330276.png" alt="image-20211211133017791"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在spring.factories中写死了springboot一启动就要给容器加载的所有配置类</span><br></pre></td></tr></table></figure>

<p>2.2按需开启自动配置项</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">虽然145个场景的所有自动配置启动时默认全部加载，但按照条件装配规则，最终会按需配置  @ConditionalXXX</span><br></pre></td></tr></table></figure>

<p>2.3修改默认配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">        <span class="meta">@Bean</span></span><br><span class="line">		<span class="meta">@ConditionalOnBean(MultipartResolver.class)</span>  <span class="comment">//容器中有这个类型组件</span></span><br><span class="line">		<span class="meta">@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)</span> <span class="comment">//容器中没有这个名字 multipartResolver 的组件</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> MultipartResolver <span class="title">multipartResolver</span><span class="params">(MultipartResolver resolver)</span> </span>&#123;</span><br><span class="line">            <span class="comment">//给@Bean标注的方法传入了对象参数，这个参数的值就会从容器中找。</span></span><br><span class="line">            <span class="comment">//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范</span></span><br><span class="line">			<span class="comment">// Detect if the user has created a MultipartResolver but named it incorrectly</span></span><br><span class="line">			<span class="keyword">return</span> resolver;</span><br><span class="line">		&#125;</span><br><span class="line">给容器中加入了文件上传解析器；</span><br></pre></td></tr></table></figure>

<p>@RequestParam(请求参数名) : 写在方法参数上</p>
<p><strong>SpringBoot默认会在底层配好所有的组件。但是如果用户自己配置了以用户的优先</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span>    </span><br><span class="line"><span class="meta">@ConditionalOnMissingBean</span>   </span><br><span class="line"><span class="function"><span class="keyword">public</span> CharacterEncodingFilter <span class="title">characterEncodingFilter</span><span class="params">()</span> </span>&#123; &#125;</span><br></pre></td></tr></table></figure>

<p>总结：</p>
<ul>
<li>SpringBoot先加载所有的自动配置类</li>
<li>每个自动配置类按照条件进行生效，默认都会绑定配置文件指定的值。XXXProperties里面拿，XXXProperties和配置文件进行了绑定</li>
<li>生效的配置类就会给容器中装配很多组件</li>
<li>只要容器中有这些组件，相当于这些功能都有</li>
<li>只要用户有自己配置的，就以自己配置的优先</li>
<li>定制化配置<ul>
<li>用@Bean替换底层组件</li>
<li>通过配置文件修改默认配置</li>
</ul>
</li>
</ul>
<p>XXXAutoConfiguration  ===&gt; 注册组件 ===&gt; 从XXXProperties中获取 ===&gt; 从application.properties中获取</p>
<p>2.4最佳实践</p>
<ul>
<li><p>引入场景依赖</p>
</li>
<li><p>查看自动配置了哪些</p>
<ul>
<li><p>自己分析，引入场景对应的自动配置一般都生效了</p>
</li>
<li><p>配置文件中debug=true开启自动配置报告。Negative（不生效）</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202112152231205.png" alt="image-20211215223115152"></p>
</li>
</ul>
</li>
<li><p>是否需要修改</p>
<ul>
<li>参照文档修改配置项</li>
<li>自定义加入或者替换组件</li>
<li>自定义器XXXCustomizer</li>
<li>……</li>
</ul>
</li>
</ul>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">spring.banner.image.location</span>=<span class="string">classpath:banner.jpg  #修改SpringBoot启动页面</span></span><br></pre></td></tr></table></figure>

<h2 id="4-总结"><a href="#4-总结" class="headerlink" title="4.总结"></a>4.总结</h2><p>➢ Spring Boot 的父级依赖 spring-boot-starter-parent 配置之后，当前的项目就是 Spring Boot 项目</p>
<p>➢ spring-boot-starter-parent 是一个 Springboot 的父级依赖，开发 SpringBoot 程序都需要继承该父级项目，它用来提供相关的 Maven 默认依赖，使用它之后，常用的 jar包依赖可以省去 version 配置</p>
<p>➢ Spring Boot 提供了哪些默认 jar 包的依赖，可查看该父级依赖的 pom 文件</p>
<p>➢ 如果不想使用某个默认的依赖版本，可以通过 pom.xml 文件的属性配置覆盖各个依赖项，比如覆盖 Spring 版本</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line"> 	<span class="tag">&lt;<span class="name">spring-framework.version</span>&gt;</span>5.0.0.RELEASE&lt;/ spring-framework.version &gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>➢ <strong>@SpringBootApplication</strong> <strong>注解是</strong> <strong>Spring Boot</strong> <strong>项目的核心注解，主要作用是开启Spring 自动配置，如果在Application 类上去掉该注解，那么不会启动 SpringBoot程序</strong></p>
<p>➢ main 方法是一个标准的 Java 程序的 main 方法，主要作用是作为项目启动运行的入口 </p>
<p>➢ @Controller 及 @ResponseBody 依然是我们之前的 Spring MVC，因为 Spring Boot的里面依然是使用我们的 Spring MVC + Spring + MyBatis 等框架</p>
<h1 id="二、dev-tools（热部署工具）"><a href="#二、dev-tools（热部署工具）" class="headerlink" title="二、dev-tools（热部署工具）"></a>二、dev-tools（热部署工具）</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-devtools<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>项目或配置文件就该后按ctrl+f9即可</p>
<h1 id="三、Spring-Boot的核心配置文件"><a href="#三、Spring-Boot的核心配置文件" class="headerlink" title="三、Spring Boot的核心配置文件"></a>三、Spring Boot的核心配置文件</h1><p>Spring Boot 的核心配置文件用于配置 Spring Boot 程序，名字必须以 application 开始</p>
<h2 id="3-1核心配置格式"><a href="#3-1核心配置格式" class="headerlink" title="3.1核心配置格式"></a>3.1核心配置格式</h2><h3 id="3-1-1-properties-文件（默认采用该文件）"><a href="#3-1-1-properties-文件（默认采用该文件）" class="headerlink" title="3.1.1 properties 文件（默认采用该文件）"></a>3.1.1 properties 文件（默认采用该文件）</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置内嵌Tomcat的端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">8081</span></span><br><span class="line"><span class="comment">#设置上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/springboot</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202212208273.png" alt="image-20220221220841918"></p>
<h3 id="3-1-2-yaml配置文件"><a href="#3-1-2-yaml配置文件" class="headerlink" title="3.1.2.yaml配置文件"></a>3.1.2.yaml配置文件</h3><h4 id="1-yaml基础语法"><a href="#1-yaml基础语法" class="headerlink" title="1.yaml基础语法"></a>1.yaml基础语法</h4><p>语法结构 ：key：空格 value</p>
<p>说明：语法要求严格！</p>
<p>1、空格不能省略</p>
<p>2、<strong>以缩进来控制层级关系</strong>，只要是左边对齐的一列数据都是同一个层级的。</p>
<p>3、属性和值的大小写都是十分敏感的。</p>
<p>4、’’和””，表示转义/不转义</p>
<h4 id="2-数组存储方式"><a href="#2-数组存储方式" class="headerlink" title="2.数组存储方式"></a>2.数组存储方式</h4><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">pets:</span></span><br><span class="line">	<span class="bullet">-</span> <span class="string">dog</span></span><br><span class="line">	<span class="bullet">-</span> <span class="string">cat</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：当yaml文件和properties文件同时存在，优先读取properties文件</strong></p>
<h3 id="3-1-3-多环境配置"><a href="#3-1-3-多环境配置" class="headerlink" title="3.1.3 多环境配置"></a>3.1.3 多环境配置</h3><p>我们在主配置文件编写的时候，文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本；</p>
<p>例如：</p>
<ul>
<li><p>application-test.properties 代表测试环境配置</p>
</li>
<li><p>application-dev.properties 代表开发环境配置</p>
</li>
</ul>
<p>但是Springboot并不会直接启动这些配置文件，它<strong>默认使用application.properties主配置文件</strong>；</p>
<p>我们需要<strong>通过一个配置来选择需要激活的环境</strong>：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#比如在配置文件中指定使用dev环境，我们可以通过在不同的配置文件中设置不同的端口号进行测试；</span></span><br><span class="line"><span class="comment">#激活使用的配置文件</span></span><br><span class="line"><span class="meta">spring.profiles.active</span>=<span class="string">dev</span></span><br></pre></td></tr></table></figure>

<p>2.yaml的多文档块<br>和properties配置文件中一样，但是使用yml去实现不需要创建多个配置文件，更加方便了 !</p>
<p>profiles : 环境切换</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8081</span></span><br><span class="line"><span class="comment">#选择要激活那个环境块</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">profiles:</span></span><br><span class="line">    <span class="attr">active:</span> <span class="string">prod</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8083</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">	<span class="attr">profiles:</span>  <span class="string">dev</span> <span class="comment">#配置环境的名称</span></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8084</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">profiles:</span> <span class="string">prod</span>  <span class="comment">#配置环境的名称</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：如果yml和properties同时都配置了端口，并且没有激活其他环境 ， 默认会使用properties配置文件的！</strong><br><strong>Spring Boot</strong> <strong>自定义配置</strong></p>
<h4 id="3-配置文件占位符"><a href="#3-配置文件占位符" class="headerlink" title="3.配置文件占位符"></a>3.配置文件占位符</h4><p>配置文件还可以编写占位符生成随机数</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">person:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">yuan$&#123;random.uuid&#125;</span> <span class="comment"># 随机uuid</span></span><br><span class="line">    <span class="attr">age:</span> <span class="string">$&#123;random.int&#125;</span>  <span class="comment"># 随机int</span></span><br><span class="line">    <span class="attr">happy:</span> <span class="literal">false</span></span><br><span class="line">    <span class="attr">birth:</span> <span class="number">2000</span><span class="string">/01/01</span></span><br><span class="line">    <span class="attr">maps:</span> &#123;<span class="attr">k1:</span> <span class="string">v1</span>,<span class="attr">k2:</span> <span class="string">v2</span>&#125;</span><br><span class="line">    <span class="attr">lists:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">code</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">girl</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">music</span></span><br><span class="line">    <span class="attr">dog:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">$&#123;person.hello:other&#125;_旺财</span>  <span class="string">//如果person.hello存在，则用该值拼接</span></span><br><span class="line">      <span class="attr">age:</span> <span class="number">1</span></span><br></pre></td></tr></table></figure>

<h3 id="3-1-4-SpringBoot自定义配置"><a href="#3-1-4-SpringBoot自定义配置" class="headerlink" title="3.1.4.SpringBoot自定义配置"></a>3.1.4.SpringBoot自定义配置</h3><p>在 SpringBoot 的核心配置文件中，除了使用内置的配置项之外，我们还可以在自定义配置，然后采用如下注解去读取配置的属性值</p>
<h4 id="1-Value注解"><a href="#1-Value注解" class="headerlink" title="1.@Value注解"></a>1.@Value注解</h4><p>用于逐个读取 application.properties 中的配置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Value($&#123;school.name&#125;)</span></span><br><span class="line"><span class="keyword">private</span> String schoolName;</span><br></pre></td></tr></table></figure>

<p>之后我们就可以通过application.properties配置文件为该属性赋值了。</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221022999.png" alt="image-20220222102222121"></p>
<h4 id="2-ConfigurationProperties"><a href="#2-ConfigurationProperties" class="headerlink" title="2.@ConfigurationProperties"></a>2.@ConfigurationProperties</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    @ConfigurationProperties作用：</span></span><br><span class="line"><span class="comment">    	将配置文件中配置的每一个属性的值，映射到这个组件中；</span></span><br><span class="line"><span class="comment">    	告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定</span></span><br><span class="line"><span class="comment">    	参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>警告解决</p>
</blockquote>
<p>➢ 在 ConfigInfo 类中使用了 ConfigurationProperties 注解后，IDEA 会出现一个警告，不影响程序的执行</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221007512.png" alt="image-20220222100728129"></p>
<p>➢ 点击 <strong>打开文档</strong> 跳转到网页，在网页中提示需要加一个依赖，我们将这个依赖拷贝，粘贴到 pom.xml 文件中</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221008963.png" alt="image-20220222100836609"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--解决使用@ConfigurationProperties 注解出现警告问题--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-configuration-processor<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>添加完依赖之后，需要重新运行SpringBoot</p>
<p><strong>@PropertySource ：</strong>加载指定的配置文件；</p>
<blockquote>
<p>中文乱码</p>
</blockquote>
<p>如果在 SpringBoot 核心配置文件中有中文信息，会出现乱码：</p>
<ul>
<li>一般在配置文件中，不建议出现中文（注释除外）</li>
<li>如果有，可以先转化为 ASCII 码 </li>
</ul>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221014005.png" alt="image-20220222101418284"></p>
<blockquote>
<p>友情提示</p>
</blockquote>
<p><strong>如果是从其它地方拷贝的配置文件，一定要将里面的空格删干净</strong></p>
<h4 id="3-对比"><a href="#3-对比" class="headerlink" title="3.对比"></a>3.对比</h4><table>
<thead>
<tr>
<th></th>
<th>@Configuration</th>
<th>@Value</th>
</tr>
</thead>
<tbody><tr>
<td>功能</td>
<td>批量注入配置文件中的属性</td>
<td>一个个指定</td>
</tr>
<tr>
<td>松散绑定（松散语法）</td>
<td>支持</td>
<td>不支持</td>
</tr>
<tr>
<td>SpEL</td>
<td>不支持</td>
<td>支持</td>
</tr>
<tr>
<td>JSR303数据校验</td>
<td>支持</td>
<td>不支持</td>
</tr>
<tr>
<td>复杂类型封装</td>
<td>支持</td>
<td>不支持</td>
</tr>
</tbody></table>
<ol>
<li>松散绑定：这个什么意思呢? 比如我的yml中写的last-name，这个和lastName是一样的， 后面跟着的字母默认是大写的。这就是松散绑定。可以测试一下</li>
<li> JSR303数据校验 ， 这个就是我们可以在字段是增加一层过滤器验证 ， 可以保证数据的合法性</li>
<li>复杂类型封装，yml中可以封装对象 ， 使用@value就不支持</li>
</ol>
<h2 id="3-2-SpringBoot前端使用jsp"><a href="#3-2-SpringBoot前端使用jsp" class="headerlink" title="3.2.SpringBoot前端使用jsp"></a>3.2.SpringBoot前端使用jsp</h2><p>新建springboot工程</p>
<h3 id="3-2-1-在pom-xml文件中配置以下依赖项"><a href="#3-2-1-在pom-xml文件中配置以下依赖项" class="headerlink" title="3.2.1.在pom.xml文件中配置以下依赖项"></a>3.2.1.在pom.xml文件中配置以下依赖项</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--引入 Spring Boot 内嵌的 Tomcat 对 JSP 的解析包，不加解析不了 jsp 页面--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果只是使用 JSP 页面，可以只添加该依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.embed<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat-embed-jasper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果要使用 servlet 必须添加该以下两个依赖--&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- servlet 依赖的 jar 包--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javax.servlet-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet.jsp<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javax.servlet.jsp-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果使用 JSTL 必须添加该依赖--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--jstl 标签依赖的 jar 包 start--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jstl<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-2-在pom-xml的build标签中要配置以下信息"><a href="#3-2-2-在pom-xml的build标签中要配置以下信息" class="headerlink" title="3.2.2.在pom.xml的build标签中要配置以下信息"></a>3.2.2.在pom.xml的build标签中要配置以下信息</h3><p>SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources 目录下才能访问，否则访问不到。其实官方已经更建议使用模板技术（Thymeleaf）</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221101151.png" alt="image-20220222110103094"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--源文件位置--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--指定编译到 META-INF/resources，该目录不能随便写--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--指定要把哪些文件编译进去，**表示 webapp 目录及子目录，*.*表示所有文件--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-3-在application-properties文件中配置SpringMVC视图解析器"><a href="#3-2-3-在application-properties文件中配置SpringMVC视图解析器" class="headerlink" title="3.2.3.在application.properties文件中配置SpringMVC视图解析器"></a>3.2.3.在application.properties文件中配置SpringMVC视图解析器</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置SpringMVC视图解析器</span></span><br><span class="line"><span class="comment">#其中：/ 表示目录为 src/main/webapp</span></span><br><span class="line"><span class="meta">spring.mvc.view.prefix</span>=<span class="string">/</span></span><br><span class="line"><span class="meta">spring.mvc.view.suffix</span>=<span class="string">.jsp</span></span><br></pre></td></tr></table></figure>

<h3 id="3-2-4-创建JspController类"><a href="#3-2-4-创建JspController类" class="headerlink" title="3.2.4.创建JspController类"></a>3.2.4.创建JspController类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JspController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">(HttpServletRequest request)</span></span>&#123;</span><br><span class="line">        request.setAttribute(<span class="string">&quot;jsp&quot;</span>,<span class="string">&quot;hello,jsp&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-2-5在src-main-下创建一个webapp目录，然后在该目录下新建index-jsp页面"><a href="#3-2-5在src-main-下创建一个webapp目录，然后在该目录下新建index-jsp页面" class="headerlink" title="3.2.5在src/main 下创建一个webapp目录，然后在该目录下新建index.jsp页面"></a>3.2.5在src/main 下创建一个webapp目录，然后在该目录下新建index.jsp页面</h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221050325.png" alt="image-20220222105057538"></p>
<h1 id="四、SpringBoot框架web开发"><a href="#四、SpringBoot框架web开发" class="headerlink" title="四、SpringBoot框架web开发"></a>四、SpringBoot框架web开发</h1><h2 id="4-1-Spring-Boot集成Mybatis"><a href="#4-1-Spring-Boot集成Mybatis" class="headerlink" title="4.1.Spring Boot集成Mybatis"></a>4.1.Spring Boot集成Mybatis</h2><p>新建项目：springboot-mybatis</p>
<h3 id="4-1-1-实现步骤"><a href="#4-1-1-实现步骤" class="headerlink" title="4.1.1.实现步骤"></a>4.1.1.实现步骤</h3><p>（1）建库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database springboot</span><br></pre></td></tr></table></figure>

<p>（2）建表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> t_student;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> t_student </span><br><span class="line">(</span><br><span class="line">   id                   <span class="type">INT</span>(<span class="number">10</span>)                        <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">   NAME                 <span class="type">VARCHAR</span>(<span class="number">20</span>)                    <span class="keyword">NULL</span>,</span><br><span class="line">   age                  <span class="type">INT</span>(<span class="number">10</span>)                        <span class="keyword">NULL</span>,</span><br><span class="line">   <span class="keyword">CONSTRAINT</span> PK_T_STUDENT <span class="keyword">PRIMARY</span> KEY clustered (id)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>（3）添加数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;zhangsan&quot;,<span class="number">25</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;lisi&quot;,<span class="number">28</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;wangwu&quot;,<span class="number">23</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;Tom&quot;,<span class="number">21</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;Jck&quot;,<span class="number">55</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;Lucy&quot;,<span class="number">27</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> t_student(NAME,age) <span class="keyword">VALUES</span>(&quot;zhaoliu&quot;,<span class="number">75</span>);</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221112526.png" alt="image-20220222111237118"></p>
<h3 id="4-1-2-在-pom-xml-中添加相关jar-依赖"><a href="#4-1-2-在-pom-xml-中添加相关jar-依赖" class="headerlink" title="4.1.2.在 pom.xml 中添加相关jar 依赖"></a>4.1.2.<strong>在</strong> <strong>pom.xml</strong> 中添加相关jar <strong>依赖</strong></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--MyBatis 整合 SpringBoot 的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--MySQL 的驱动依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="4-1-3在-Springboot-的核心配置文件application-properties中配置数据源"><a href="#4-1-3在-Springboot-的核心配置文件application-properties中配置数据源" class="headerlink" title="4.1.3在 Springboot 的核心配置文件application.properties中配置数据源"></a>4.1.3在 <strong>Springboot</strong> 的核心配置文件application.properties中配置数据源</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置数据库的连接信息</span></span><br><span class="line"><span class="comment">#注意这里的驱动类有变化</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://localhost:3306/springboot?useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">123456</span></span><br></pre></td></tr></table></figure>



<h3 id="4-1-3-Mybatis逆向工程"><a href="#4-1-3-Mybatis逆向工程" class="headerlink" title="4.1.3.Mybatis逆向工程"></a>4.1.3.Mybatis逆向工程</h3><p>1.<strong>拷贝</strong> <strong>Mybatis</strong> <strong>反向工程配置文件到项目的根目录下</strong></p>
<p>获取目录：GeneratorMapper.xml</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221425552.png" alt="image-20220222142550216"></p>
<p>2.<strong>根据项目及表的情况，修改</strong> <strong>GeneratorMapper.xml</strong> <strong>配置</strong></p>
<p>红色标准的地方是需要确认修改的地方，尤其注意</p>
<p>➢ <strong>如果使用高版本，驱动类变为：com.mysql.cj.jdbc.Driver</strong></p>
<p>➢ <strong>url</strong> <strong>后面应该加属性</strong> <strong>nullCatalogMeansCurrent=true，否则生成有问题</strong></p>
<p><strong>当前版本</strong> <strong>MySQL</strong> <strong>数据库为5.5</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">generatorConfiguration</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">generatorConfiguration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 指定连接数据库的 JDBC 驱动包所在位置，指定到你本机的完整路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">classPathEntry</span> <span class="attr">location</span>=<span class="string">&quot;E:\Maven\apache-maven-3.8.2-bin\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 配置 table 表信息内容体，targetRuntime 指定采用 MyBatis3 的版本 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context</span> <span class="attr">id</span>=<span class="string">&quot;tables&quot;</span> <span class="attr">targetRuntime</span>=<span class="string">&quot;MyBatis3&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 抑制生成注释，由于生成的注释都是英文的，可以不让它生成 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suppressAllComments&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置数据库连接信息 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbcConnection</span> <span class="attr">driverClass</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">connectionURL</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/springboot&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">userId</span>=<span class="string">&quot;root&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jdbcConnection</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 model 类，targetPackage 指定 model 类的包名， targetProject 指定</span></span><br><span class="line"><span class="comment">       生成的 model 放在 eclipse 的哪个工程下面--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaModelGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.bean&quot;</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;trimStrings&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaModelGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper.xml 文件，targetPackage 指定 mapper.xml 文件的</span></span><br><span class="line"><span class="comment">       包名， targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sqlMapGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span></span></span><br><span class="line"><span class="tag">                         <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sqlMapGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包</span></span><br><span class="line"><span class="comment">       名， targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaClientGenerator</span> <span class="attr">type</span>=<span class="string">&quot;XMLMAPPER&quot;</span></span></span><br><span class="line"><span class="tag">                             <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaClientGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 数据库表名及对应的 Java 模型类名 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">&quot;t_student&quot;</span> <span class="attr">domainObjectName</span>=<span class="string">&quot;Student&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableCountByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableUpdateByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableDeleteByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">enableSelectByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">selectByExampleQueryId</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">generatorConfiguration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221438770.png" alt="image-20220222143810665"></p>
<p>报红无影响</p>
<p>2.<strong>在</strong> <strong>pom.xml</strong> <strong>文件中添加</strong> <strong>mysql</strong> <strong>反向工程依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--mybatis 代码自动生成插件--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.generator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-generator-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置文件的位置--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">configurationFile</span>&gt;</span>GeneratorMapper.xml<span class="tag">&lt;/<span class="name">configurationFile</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">verbose</span>&gt;</span>true<span class="tag">&lt;/<span class="name">verbose</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">overwrite</span>&gt;</span>true<span class="tag">&lt;/<span class="name">overwrite</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><font color='red'>注意：mybatis逆向工程只针对于单表操作</font></p>
<p>mybatis逆向工程映射的xml配置文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mapper</span> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span> <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.atyuan.mapper.StudentMapper&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">  resultMap的作用：</span></span><br><span class="line"><span class="comment">      1.当数据库中字段名称与实体类对象的属性名不一致时，可以进行转换</span></span><br><span class="line"><span class="comment">      2.当前查询的结果没有对应一个表的时候，可以自定义一个结果集</span></span><br><span class="line"><span class="comment">  --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">&quot;BaseResultMap&quot;</span> <span class="attr">type</span>=<span class="string">&quot;com.atyuan.model.Student&quot;</span>&gt;</span></span><br><span class="line">      <span class="comment">&lt;!--id 标签只能修饰主键字段--&gt;</span></span><br><span class="line">      <span class="comment">&lt;!--result 除了主键意外的字段--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">          column 数据库中的字段名称</span></span><br><span class="line"><span class="comment">          property 映射对象的属性名称</span></span><br><span class="line"><span class="comment">          jdbcType 列在数据库中字段的类型(可以省略不写)</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">&quot;id&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;INTEGER&quot;</span> <span class="attr">property</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;name&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;VARCHAR&quot;</span> <span class="attr">property</span>=<span class="string">&quot;name&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">&quot;age&quot;</span> <span class="attr">jdbcType</span>=<span class="string">&quot;INTEGER&quot;</span> <span class="attr">property</span>=<span class="string">&quot;age&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">      sql语句片段，将公告的部分抽取出来</span></span><br><span class="line"><span class="comment">      通过include标签引用sql语句片段</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">&quot;Base_Column_List&quot;</span>&gt;</span></span><br><span class="line">      id, name, age</span><br><span class="line">    <span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectByPrimaryKey&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;java.lang.Integer&quot;</span> <span class="attr">resultMap</span>=<span class="string">&quot;BaseResultMap&quot;</span>&gt;</span></span><br><span class="line">        select</span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">&quot;Base_Column_List&quot;</span> /&gt;</span></span><br><span class="line">        from t_student</span><br><span class="line">        where id = #&#123;id,jdbcType=INTEGER&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">&quot;deleteByPrimaryKey&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;java.lang.Integer&quot;</span>&gt;</span></span><br><span class="line">        delete from t_student</span><br><span class="line">        where id = #&#123;id,jdbcType=INTEGER&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">delete</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;insert&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.atyuan.model.Student&quot;</span>&gt;</span></span><br><span class="line">        insert into t_student (id, name, age</span><br><span class="line">          )</span><br><span class="line">        values (#&#123;id,jdbcType=INTEGER&#125;, #&#123;name,jdbcType=VARCHAR&#125;, #&#123;age,jdbcType=INTEGER&#125;</span><br><span class="line">          )</span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">&quot;insertSelective&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.atyuan.model.Student&quot;</span>&gt;</span></span><br><span class="line">        insert into t_student</span><br><span class="line">        <span class="tag">&lt;<span class="name">trim</span> <span class="attr">prefix</span>=<span class="string">&quot;(&quot;</span> <span class="attr">suffix</span>=<span class="string">&quot;)&quot;</span> <span class="attr">suffixOverrides</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;id != null&quot;</span>&gt;</span></span><br><span class="line">            id,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name != null&quot;</span>&gt;</span></span><br><span class="line">            name,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age != null&quot;</span>&gt;</span></span><br><span class="line">            age,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">trim</span> <span class="attr">prefix</span>=<span class="string">&quot;values (&quot;</span> <span class="attr">suffix</span>=<span class="string">&quot;)&quot;</span> <span class="attr">suffixOverrides</span>=<span class="string">&quot;,&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;id != null&quot;</span>&gt;</span></span><br><span class="line">            #&#123;id,jdbcType=INTEGER&#125;,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name != null&quot;</span>&gt;</span></span><br><span class="line">            #&#123;name,jdbcType=VARCHAR&#125;,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age != null&quot;</span>&gt;</span></span><br><span class="line">            #&#123;age,jdbcType=INTEGER&#125;,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">trim</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateByPrimaryKeySelective&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.atyuan.model.Student&quot;</span>&gt;</span></span><br><span class="line">        update t_student</span><br><span class="line">        <span class="tag">&lt;<span class="name">set</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;name != null&quot;</span>&gt;</span></span><br><span class="line">            name = #&#123;name,jdbcType=VARCHAR&#125;,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">&quot;age != null&quot;</span>&gt;</span></span><br><span class="line">            age = #&#123;age,jdbcType=INTEGER&#125;,</span><br><span class="line">          <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br><span class="line">        where id = #&#123;id,jdbcType=INTEGER&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;updateByPrimaryKey&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;com.atyuan.model.Student&quot;</span>&gt;</span></span><br><span class="line">        update t_student</span><br><span class="line">        set name = #&#123;name,jdbcType=VARCHAR&#125;,</span><br><span class="line">          age = #&#123;age,jdbcType=INTEGER&#125;</span><br><span class="line">        where id = #&#123;id,jdbcType=INTEGER&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">如果数据库中字段名称由多个单词构成，通过mybatis逆向工程生成的对象属性会按照驼峰命名法规则生成属性名称</span><br><span class="line">其中：数据库中字段名称由多个单词构成的时候必须使用_下划线分隔</span><br></pre></td></tr></table></figure>

<p>ORM：对象关系映射</p>
<h3 id="4-1-4-测试"><a href="#4-1-4-测试" class="headerlink" title="4.1.4.测试"></a>4.1.4.测试</h3><h4 id="1-在-web-包下创建-StudentController-并编写代码"><a href="#1-在-web-包下创建-StudentController-并编写代码" class="headerlink" title="1.在 web 包下创建 StudentController 并编写代码"></a>1.在 web 包下创建 StudentController 并编写代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentService studentService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/springBoot/student&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> <span class="meta">@ResponseBody</span> <span class="function">Object <span class="title">student</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Student student = studentService.queryStudentById(<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> student;</span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="2-在-service-包下创建-StudentService-接口并编写代码"><a href="#2-在-service-包下创建-StudentService-接口并编写代码" class="headerlink" title="2.在 service 包下创建 StudentService 接口并编写代码"></a>2.在 service 包下创建 StudentService 接口并编写代码</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据学生标识获取学生详情</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="function">Student <span class="title">queryStudentById</span><span class="params">(Integer id)</span></span>; &#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-在service-impl包下创建service接口的实现类"><a href="#3-在service-impl包下创建service接口的实现类" class="headerlink" title="3.在service.impl包下创建service接口的实现类"></a>3.在service.impl包下创建service接口的实现类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentServiceImpl</span> <span class="keyword">implements</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentMapper studentMapper;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Student <span class="title">queryStudentById</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> studentMapper.selectByPrimaryKey(id);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-在-Mybatis-反向工程生成的-StudentMapper-接口上加一个-Mapper-注解"><a href="#4-在-Mybatis-反向工程生成的-StudentMapper-接口上加一个-Mapper-注解" class="headerlink" title="4.在 Mybatis 反向工程生成的 StudentMapper 接口上加一个 Mapper 注解"></a>4.在 Mybatis 反向工程生成的 StudentMapper 接口上加一个 Mapper 注解</h4><p>@Mapper 作用：mybatis 自动扫描数据持久层的映射文件及 DAO 接口的关系</p>
<p><strong>注意：默认情况下，Mybatis 的 xml 映射文件不会编译到 target 的 class 目录下，所以我们需要在 pom.xml 文件中配置 resource</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>启动</strong> <strong>Application</strong> <strong>应用，浏览器访问测试运行</strong></p>
<h3 id="4-1-5-DAO其它开发方式"><a href="#4-1-5-DAO其它开发方式" class="headerlink" title="4.1.5.DAO其它开发方式"></a>4.1.5.DAO其它开发方式</h3><h4 id="1-在-运-行-的-主-类-上-添-加-注-解-包-扫-描-MapperScan-“com-abc-springboot-mapper”"><a href="#1-在-运-行-的-主-类-上-添-加-注-解-包-扫-描-MapperScan-“com-abc-springboot-mapper”" class="headerlink" title="1.在 运 行 的 主 类 上 添 加 注 解 包 扫 描@MapperScan(“com.abc.springboot.mapper”)"></a>1.在 运 行 的 主 类 上 添 加 注 解 包 扫 描@MapperScan(“com.abc.springboot.mapper”)</h4><ol>
<li>注释掉 StudentMapper 接口上的@Mapper 注解</li>
<li>在运行主类 Application 上加@MapperScan(“com.abc.springboot.mapper”)</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@MapperScan(&quot;com.abc.springboot.mapper&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<p>或</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="comment">//Mybatis 提供的注解：扫描数据持久层的 mapper 映谢配置文件,DAO 接口上就不用加@Mapper</span></span><br><span class="line"><span class="comment">//basePackages 通常指定到数据持久层包即可</span></span><br><span class="line"><span class="meta">@MapperScan(basePackages = &quot;com.abc.springboot.mapper&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-将接口和映射文件分开"><a href="#2-将接口和映射文件分开" class="headerlink" title="2.将接口和映射文件分开"></a>2.将接口和映射文件分开</h4><p>1）在resources资源目录下新建mapper目录，然后将原先的StudentMapper.xml映射文件放到目录中</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221925612.png" alt="image-20220222192532149"></p>
<p>2）在 application.properties 配置文件中指定映射文件的位置，这个配置只有接口和映射文件不在同一个包的情况下，才需要指定Mybatis映射文件的路径</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定 Mybatis 映射文件的路径</span></span><br><span class="line"><span class="meta">mybatis.mapper-locations</span>=<span class="string">classpath:mapper/*.xml</span></span><br></pre></td></tr></table></figure>

<h2 id="4-2-Spring-Boot事务支持"><a href="#4-2-Spring-Boot事务支持" class="headerlink" title="4.2.Spring Boot事务支持"></a>4.2.Spring Boot事务支持</h2><p>Spring Boot使用事务非常简单，底层依然采用的是 Spring 本身提供的事务管理</p>
<p>➢ 在入口类中使用注解 @EnableTransactionManagement 开启事务支持</p>
<p>➢ 在访问数据库的 Service 方法上添加注解 @Transactional 即可</p>
<h3 id="4-2-1-案例思路"><a href="#4-2-1-案例思路" class="headerlink" title="4.2.1.案例思路"></a>4.2.1.案例思路</h3><p>通过 SpringBoot +MyBatis 实现对数据库学生表的更新操作，在 service 层的方法中构建异常，查看事务是否生效</p>
<h3 id="4-2-2-实现步骤"><a href="#4-2-2-实现步骤" class="headerlink" title="4.2.2.实现步骤"></a>4.2.2.实现步骤</h3><h4 id="1-在StudentController中添加更新学生信息的办法"><a href="#1-在StudentController中添加更新学生信息的办法" class="headerlink" title="1.在StudentController中添加更新学生信息的办法"></a>1.在StudentController中添加更新学生信息的办法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/springboot/modify&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@ResponseBody</span> <span class="function">Object <span class="title">modifyStudent</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        Student student = <span class="keyword">new</span> Student();</span><br><span class="line">        student.setId(<span class="number">1</span>);</span><br><span class="line">        student.setName(<span class="string">&quot;Jack&quot;</span>);</span><br><span class="line">        student.setAge(<span class="number">33</span>);</span><br><span class="line">        count = studentService.modifyStudentById(student);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;fail&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> count; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-在StudentService接口中添加更新学生信息的方法"><a href="#2-在StudentService接口中添加更新学生信息的方法" class="headerlink" title="2.在StudentService接口中添加更新学生信息的方法"></a>2.在StudentService接口中添加更新学生信息的方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">modifyStudentById</span><span class="params">(Student student)</span></span>;</span><br></pre></td></tr></table></figure>

<h4 id="3-在StudentServiceImpl接口实现类中对更新学生方法进行实现，并构建一个异常，同时在该方法上加-Transactional注解"><a href="#3-在StudentServiceImpl接口实现类中对更新学生方法进行实现，并构建一个异常，同时在该方法上加-Transactional注解" class="headerlink" title="3.在StudentServiceImpl接口实现类中对更新学生方法进行实现，并构建一个异常，同时在该方法上加@Transactional注解"></a>3.在StudentServiceImpl接口实现类中对更新学生方法进行实现，并构建一个异常，同时在该方法上加@Transactional注解</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Transactional</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">modifyStudentById</span><span class="params">(Student student)</span> </span>&#123;</span><br><span class="line">    intstudentMapper.updateByPrimaryKeySelective(student);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-启动Application，通过浏览器访问进行测试"><a href="#4-启动Application，通过浏览器访问进行测试" class="headerlink" title="4.启动Application，通过浏览器访问进行测试"></a>4.启动Application，通过浏览器访问进行测试</h4><p><strong>浏览器输出：fail</strong></p>
<p><strong>控制台</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221951922.png" alt="image-20220222195122107"></p>
<p><strong>数据库</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221952434.png" alt="image-20220222195204451"></p>
<p><strong>上述结果说明事务起作用了</strong></p>
<h4 id="5-将实现类方法上的-Transactional注释掉再做测试"><a href="#5-将实现类方法上的-Transactional注释掉再做测试" class="headerlink" title="5.将实现类方法上的@Transactional注释掉再做测试"></a>5.将实现类方法上的@Transactional注释掉再做测试</h4><p>数据库表中的数据被更新了</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202221954921.png" alt="image-20220222195452373"></p>
<h2 id="4-3-SpringBoot下的SpringMVC"><a href="#4-3-SpringBoot下的SpringMVC" class="headerlink" title="4.3.SpringBoot下的SpringMVC"></a>4.3.SpringBoot下的SpringMVC</h2><p>注解的用法与之前笔记中的SpringMVC一样，可参考之前的笔记</p>
<h2 id="4-4-Spring-Boot实现RESTful"><a href="#4-4-Spring-Boot实现RESTful" class="headerlink" title="4.4.Spring Boot实现RESTful"></a>4.4.Spring Boot实现RESTful</h2><blockquote>
<p>常用注解</p>
</blockquote>
<p>@PathVariable: 路径变量</p>
<p>@RequestHeader：获取请求头</p>
<p>@RequestParam：获取请求参数</p>
<p>@MatrixVariable：矩阵变量，请求格式为  /路径;参数1=参数值，参数2=参数值</p>
<p>@CookieValue：获取Cookie值</p>
<p>@RequestBody：获取请求体【post】</p>
<p>@RequestAttribute:从请求域中获取数据 ===等价于===&gt;request.getAttribute(“属性名”)</p>
<p><font color='red'><strong>该注解是实现RESTFul最主要的一个注解</strong></font></p>
<h3 id="4-4-1-请求冲突问题"><a href="#4-4-1-请求冲突问题" class="headerlink" title="4.4.1.请求冲突问题"></a>4.4.1.请求冲突问题</h3><h4 id="1-创建RestfulController类"><a href="#1-创建RestfulController类" class="headerlink" title="1.创建RestfulController类"></a>1.创建RestfulController类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping(value = &quot;/springBoot/order/&#123;id&#125;/&#123;status&#125;&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">queryOrder</span><span class="params">(<span class="meta">@PathVariable(&quot;id&quot;)</span> Integer id,</span></span></span><br><span class="line"><span class="params"><span class="function">                         <span class="meta">@PathVariable(&quot;status&quot;)</span> Integer status)</span> </span>&#123;</span><br><span class="line">    ....</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@GetMapping(value = &quot;/springBoot/&#123;id&#125;/order/&#123;status&#125;&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">queryOrder1</span><span class="params">(<span class="meta">@PathVariable(&quot;id&quot;)</span> Integer id,</span></span></span><br><span class="line"><span class="params"><span class="function">                          <span class="meta">@PathVariable(&quot;status&quot;)</span> Integer status)</span> </span>&#123;</span><br><span class="line">    ....</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">虽然两个路径写法改变了，但是由于传递的两个参数都是 <span class="keyword">int</span> 值，所以不知道该交给哪个请求进行处理，就会出现匹配模糊不清的异常，所以要想解决冲突，有两种方式：</span><br><span class="line">* <span class="number">1.</span>修改请求路径</span><br><span class="line">* <span class="number">2.</span>修改请求方式</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222024291.png" alt="image-20220222202430985"></p>
<h3 id="4-4-2-RESTful原则"><a href="#4-4-2-RESTful原则" class="headerlink" title="4.4.2.RESTful原则"></a>4.4.2.RESTful原则</h3><p>➢ 增 post 请求、删 delete 请求、改 put 请求、查 get 请求</p>
<p>➢ <strong>请求路径不要出现动词</strong></p>
<p>例如：查询订单接口</p>
<p>/boot/order/1021/1（推荐）</p>
<p>/boot/queryOrder/1021/1（不推荐）</p>
<p>➢ <strong>分页、排序等操作，不需要使用斜杠传参数</strong></p>
<p>例如：订单列表接口</p>
<p>/boot/orders?page=1&amp;sort=desc</p>
<p>一般传的参数不是数据库表的字段，可以不采用斜杠</p>
<h3 id="4-4-3-创建ParameterTestController"><a href="#4-4-3-创建ParameterTestController" class="headerlink" title="4.4.3.创建ParameterTestController"></a>4.4.3.创建ParameterTestController</h3><p>SpringBoot默认是禁用了矩阵变量的功能</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ParameterTestController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//  car/2/owner/zhangsan</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/car/&#123;id&#125;/owner/&#123;username&#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String,Object&gt; <span class="title">getCar</span><span class="params">(<span class="meta">@PathVariable(&quot;id&quot;)</span> Integer id,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@PathVariable(&quot;username&quot;)</span> String name,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@PathVariable</span> Map&lt;String,String&gt; pv,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@RequestHeader(&quot;User-Agent&quot;)</span> String userAgent,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@RequestHeader</span> Map&lt;String,String&gt; header,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@RequestParam(&quot;age&quot;)</span> Integer age,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@RequestParam(&quot;inters&quot;)</span> List&lt;String&gt; inters,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@RequestParam</span> Map&lt;String,String&gt; params,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@CookieValue(&quot;_ga&quot;)</span> String _ga,</span></span></span><br><span class="line"><span class="params"><span class="function">                                     <span class="meta">@CookieValue(&quot;_ga&quot;)</span> Cookie cookie)</span></span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line"><span class="comment">//        map.put(&quot;id&quot;,id);</span></span><br><span class="line"><span class="comment">//        map.put(&quot;name&quot;,name);</span></span><br><span class="line"><span class="comment">//        map.put(&quot;pv&quot;,pv);</span></span><br><span class="line"><span class="comment">//        map.put(&quot;userAgent&quot;,userAgent);</span></span><br><span class="line"><span class="comment">//        map.put(&quot;headers&quot;,header);</span></span><br><span class="line">        map.put(<span class="string">&quot;age&quot;</span>,age);</span><br><span class="line">        map.put(<span class="string">&quot;inters&quot;</span>,inters);</span><br><span class="line">        map.put(<span class="string">&quot;params&quot;</span>,params);</span><br><span class="line">        map.put(<span class="string">&quot;_ga&quot;</span>,_ga);</span><br><span class="line">        System.out.println(cookie.getName()+<span class="string">&quot;===&gt;&quot;</span>+cookie.getValue());</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping(&quot;/save&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map <span class="title">postMethod</span><span class="params">(<span class="meta">@RequestBody</span> String content)</span></span>&#123;</span><br><span class="line">        Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">&quot;content&quot;</span>,content);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//1、语法： 请求路径：/cars/sell;low=34;brand=byd,audi,yd</span></span><br><span class="line">    <span class="comment">//2、SpringBoot默认是禁用了矩阵变量的功能</span></span><br><span class="line">    <span class="comment">//      手动开启：原理。对于路径的处理。UrlPathHelper进行解析。</span></span><br><span class="line">    <span class="comment">//              removeSemicolonContent（移除分号内容）支持矩阵变量的</span></span><br><span class="line">    <span class="comment">//3、矩阵变量必须有url路径变量才能被解析</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/cars/&#123;path&#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map <span class="title">carsSell</span><span class="params">(<span class="meta">@MatrixVariable(&quot;low&quot;)</span> Integer low,</span></span></span><br><span class="line"><span class="params"><span class="function">                        <span class="meta">@MatrixVariable(&quot;brand&quot;)</span> List&lt;String&gt; brand,</span></span></span><br><span class="line"><span class="params"><span class="function">                        <span class="meta">@PathVariable(&quot;path&quot;)</span> String path)</span></span>&#123;</span><br><span class="line">        Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        map.put(<span class="string">&quot;low&quot;</span>,low);</span><br><span class="line">        map.put(<span class="string">&quot;brand&quot;</span>,brand);</span><br><span class="line">        map.put(<span class="string">&quot;path&quot;</span>,path);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// /boss/1;age=20/2;age=10</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/boss/&#123;bossId&#125;/&#123;empId&#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Map <span class="title">boss</span><span class="params">(<span class="meta">@MatrixVariable(value = &quot;age&quot;,pathVar = &quot;bossId&quot;)</span> Integer bossAge,</span></span></span><br><span class="line"><span class="params"><span class="function">                    <span class="meta">@MatrixVariable(value = &quot;age&quot;,pathVar = &quot;empId&quot;)</span> Integer empAge)</span></span>&#123;</span><br><span class="line">        Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        map.put(<span class="string">&quot;bossAge&quot;</span>,bossAge);</span><br><span class="line">        map.put(<span class="string">&quot;empAge&quot;</span>,empAge);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>创建Person和Pet对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Pet pet;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">&#125;</span><br><span class="line">============================</span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Pet</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-4-4-自定义内容协商类"><a href="#4-4-4-自定义内容协商类" class="headerlink" title="4.4.4.自定义内容协商类"></a>4.4.4.自定义内容协商类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyMessageConvert</span> <span class="keyword">implements</span> <span class="title">HttpMessageConverter</span>&lt;<span class="title">Person</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;MediaType&gt; <span class="title">getSupportedMediaTypes</span><span class="params">(Class clazz)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> HttpMessageConverter.<span class="keyword">super</span>.getSupportedMediaTypes(clazz);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">canRead</span><span class="params">(Class clazz, MediaType mediaType)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">canWrite</span><span class="params">(Class clazz, MediaType mediaType)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;MediaType&gt; <span class="title">getSupportedMediaTypes</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Person <span class="title">read</span><span class="params">(Class clazz, HttpInputMessage inputMessage)</span> <span class="keyword">throws</span> IOException, HttpMessageNotReadableException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(Person person, MediaType contentType, HttpOutputMessage outputMessage)</span> <span class="keyword">throws</span> IOException, HttpMessageNotWritableException </span>&#123;</span><br><span class="line"><span class="comment">//自定义协议数据写出</span></span><br><span class="line">        String data = person.getName()+<span class="string">&quot;;&quot;</span>+person.getAge()+<span class="string">&quot;;&quot;</span>+person.getPet();</span><br><span class="line">        <span class="comment">//写出数据</span></span><br><span class="line">        OutputStream body = outputMessage.getBody();</span><br><span class="line">        body.write(data.getBytes());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-4-6-创建配置类"><a href="#4-4-6-创建配置类" class="headerlink" title="4.4.6.创建配置类"></a>4.4.6.创建配置类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> </span>&#123;</span><br><span class="line">    <span class="comment">//自定义filter</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HiddenHttpMethodFilter <span class="title">hiddenHttpMethodFilter</span><span class="params">()</span></span>&#123;</span><br><span class="line">        HiddenHttpMethodFilter methodFilter = <span class="keyword">new</span> HiddenHttpMethodFilter();</span><br><span class="line">        methodFilter.setMethodParam(<span class="string">&quot;_m&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> methodFilter;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//扩展SpringMvc功能</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> WebMvcConfigurer <span class="title">webMvcConfigurer</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> WebMvcConfigurer() &#123;</span><br><span class="line">            <span class="comment">//            自定义内容协商策略</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configureContentNegotiation</span><span class="params">(ContentNegotiationConfigurer configurer)</span> </span>&#123;</span><br><span class="line">                Map&lt;String, MediaType&gt; mediaTypes = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">                mediaTypes.put(<span class="string">&quot;json&quot;</span>,MediaType.APPLICATION_JSON);</span><br><span class="line">                mediaTypes.put(<span class="string">&quot;xml&quot;</span>, MediaType.APPLICATION_XML);</span><br><span class="line">                mediaTypes.put(<span class="string">&quot;gg&quot;</span>,MediaType.parseMediaType(<span class="string">&quot;application/x-admin&quot;</span>));</span><br><span class="line">                ParameterContentNegotiationStrategy strategy = <span class="keyword">new</span> ParameterContentNegotiationStrategy(mediaTypes);</span><br><span class="line">                configurer.strategies(Arrays.asList(strategy));</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//添加额外的信息转化器</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">extendMessageConverters</span><span class="params">(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters)</span> </span>&#123;</span><br><span class="line">                converters.add(<span class="keyword">new</span> MyMessageConvert());</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//使矩阵变量生效</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configurePathMatch</span><span class="params">(PathMatchConfigurer configurer)</span> </span>&#123;</span><br><span class="line">                UrlPathHelper urlPathHelper = <span class="keyword">new</span> UrlPathHelper();</span><br><span class="line">                <span class="comment">//不移除分号后的内容，矩阵变量才能生效</span></span><br><span class="line">                urlPathHelper.setRemoveSemicolonContent(<span class="keyword">false</span>);</span><br><span class="line">                configurer.setUrlPathHelper(urlPathHelper);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//添加一些自定义格式</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addFormatters</span><span class="params">(FormatterRegistry registry)</span> </span>&#123;</span><br><span class="line">                registry.addConverter(<span class="keyword">new</span> Converter&lt;String,Pet&gt;() &#123;</span><br><span class="line">                    <span class="comment">//自定义转换规则</span></span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> Pet <span class="title">convert</span><span class="params">(String source)</span> </span>&#123;</span><br><span class="line">                        <span class="keyword">if</span>(!StringUtils.isEmpty(source))&#123;</span><br><span class="line">                            Pet pet = <span class="keyword">new</span> Pet();</span><br><span class="line">                            String[] split = source.split(<span class="string">&quot;,&quot;</span>);</span><br><span class="line">                            pet.setName(split[<span class="number">0</span>]);</span><br><span class="line">                            pet.setAge(Integer.parseInt(split[<span class="number">1</span>]));</span><br><span class="line">                        &#125;</span><br><span class="line">                        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-4-7-开启内容协商功能"><a href="#4-4-7-开启内容协商功能" class="headerlink" title="4.4.7.开启内容协商功能"></a>4.4.7.开启内容协商功能</h3><p><img src="https://img-blog.csdnimg.cn/20211006235926592.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#开启参数内容协商模式</span></span><br><span class="line"><span class="meta">spring.contentnegotiation.favor-parameter</span>=<span class="string">true</span></span><br></pre></td></tr></table></figure>

<p><strong>使用方式在地址栏上，写 “路径?format=返回值类型”</strong></p>
<p><img src="https://img-blog.csdnimg.cn/20211006235424138.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p><strong>setParameterName可修改参数名</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202261635026.png" alt="image-20220226163514920"></p>
<p>发请求： <a target="_blank" rel="noopener" href="http://localhost:8080/test/person?format=json">http://localhost:8080/test/person?format=json</a>  我们会发现可以得到json数据</p>
<p>但是我们发送此请求却报406<a target="_blank" rel="noopener" href="http://localhost:8080/test/person?format=json">http://localhost:8080/test/person?format=</a>xml</p>
<p>原因就是我们没导入xml依赖支持</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.fasterxml.jackson.dataformat<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jackson-dataformat-xml<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202261641850.png" alt="image-20220226164128438"></p>
<p>默认状态下只有两个协商策略，但我们可以实现WebMvcConfiguration来手动自定义策略</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 自定义内容协商策略</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">configureContentNegotiation</span><span class="params">(ContentNegotiationConfigurer configurer)</span> </span>&#123;</span><br><span class="line">    Map&lt;String, MediaType&gt; mediaTypes = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    mediaTypes.put(<span class="string">&quot;json&quot;</span>,MediaType.APPLICATION_JSON);</span><br><span class="line">    mediaTypes.put(<span class="string">&quot;xml&quot;</span>, MediaType.APPLICATION_XML);</span><br><span class="line">    mediaTypes.put(<span class="string">&quot;gg&quot;</span>,MediaType.parseMediaType(<span class="string">&quot;application/x-admin&quot;</span>));</span><br><span class="line">    ParameterContentNegotiationStrategy strategy = <span class="keyword">new</span> ParameterContentNegotiationStrategy(mediaTypes);</span><br><span class="line">    configurer.strategies(Arrays.asList(strategy));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>这种方法不建议使用</strong>，因为它是覆盖原先的策略。一旦自定义参数协商管理器，就没有请求头协商管理器，这时如果不给参数添加数据类型，默认浏览器能接收所有类型的数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//解析请求头</span></span><br><span class="line">HeaderContentNegotiationStrategy headerStrategy = <span class="keyword">new</span> HeaderContentNegotiationStrategy();</span><br><span class="line">configurer.strategies(Arrays.asList(strategy,headerStrategy));</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202261648599.png" alt="image-20220226164846301"></p>
<p>我们发现，协商策略多了个gg的，但是显示出来的数据乱码了</p>
<blockquote>
<p>解决方法：在自定义的内容协商类里边设置它的编码</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(Person person, MediaType contentType, HttpOutputMessage outputMessage)</span> <span class="keyword">throws</span> IOException, HttpMessageNotWritableException </span>&#123;</span><br><span class="line">    <span class="comment">//自定义协议数据写出</span></span><br><span class="line">    String data = person.getName()+<span class="string">&quot;;&quot;</span>+person.getAge()+<span class="string">&quot;;&quot;</span>+person.getPet();</span><br><span class="line">    <span class="comment">//写出数据</span></span><br><span class="line">    OutputStream body = outputMessage.getBody();</span><br><span class="line">    System.out.println(<span class="string">&quot;=====convert=====&quot;</span>);</span><br><span class="line">    body.write(data.getBytes(<span class="string">&quot;gbk&quot;</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<blockquote>
<p>推荐使用配置文件增加协商策略</p>
</blockquote>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">mvc:</span></span><br><span class="line">    <span class="attr">contentnegotiation:</span></span><br><span class="line">      <span class="attr">media-types:</span> &#123;<span class="attr">gg:</span> <span class="string">application/x-admin</span>&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202261916917.png" alt="image-20220226191611607"></p>
<h2 id="4-5-Spring-Boot集成Redis"><a href="#4-5-Spring-Boot集成Redis" class="headerlink" title="4.5.Spring Boot集成Redis"></a>4.5.Spring Boot集成Redis</h2><h3 id="4-5-1-Spring-Boot集成Redis"><a href="#4-5-1-Spring-Boot集成Redis" class="headerlink" title="4.5.1.Spring Boot集成Redis"></a>4.5.1.Spring Boot集成Redis</h3><h4 id="1-案例思路"><a href="#1-案例思路" class="headerlink" title="1.案例思路"></a>1.案例思路</h4><p>完善根据学生 id 查询学生的功能，先从 redis 缓存中查找，如果找不到，再从数据库中查找，然后放到 redis 缓存中</p>
<h4 id="2-实现步骤"><a href="#2-实现步骤" class="headerlink" title="2.实现步骤"></a>2.实现步骤</h4><p><strong>a&gt;首先通过MyBatis逆向工程生成实体 bean 和数据持久层</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222052579.png" alt="image-20220222205248834"></p>
<p><strong>b&gt; 在pom.xml文件中添加redis依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 加载 spring boot redis 包 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>c&gt; Spring Boot核心配置文件</strong></p>
<blockquote>
<p><strong>完整</strong> <strong>application.properties</strong> <strong>配置文件如下</strong></p>
</blockquote>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置内嵌 Tomcat 端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">9090</span></span><br><span class="line"><span class="comment">#配置项目上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/</span></span><br><span class="line"><span class="comment">#配置连接 MySQL 数据库信息</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://192.168.227.131:3306/springboot?useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">root</span></span><br><span class="line"><span class="comment">#配置 redis 连接信息</span></span><br><span class="line"><span class="meta">spring.redis.host</span>=<span class="string">192.168.227.131</span></span><br><span class="line"><span class="meta">spring.redis.port</span>=<span class="string">6379</span></span><br><span class="line"><span class="comment">#spring.redis.password=123456</span></span><br></pre></td></tr></table></figure>

<p><strong>d&gt; 启动redis服务</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222115719.png" alt="image-20220222211542399"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost src]<span class="comment"># nohup ./redis-server ../redis.conf &amp;</span></span><br></pre></td></tr></table></figure>

<p><strong>e&gt; 在StudentMapper接口中创建selectAllStudentCount方法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Long <span class="title">selectAllStudentCount</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<p><strong>f&gt; 在Mapper的映射文件中写入相关的查询代码</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectAllStudentCount&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.lang.Long&quot;</span>&gt;</span></span><br><span class="line">    select count(*) from t_student</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>g&gt; 创建StudentService接口</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="comment">//获取总人数</span></span><br><span class="line">    <span class="function">Long <span class="title">queryAllStudentCount</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>h&gt; 在StudentServiceImpl中注入RedisTemplate</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentServiceImpl</span> <span class="keyword">implements</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentMapper studentMapper;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RedisTemplate&lt;Object,Object&gt; redisTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Long <span class="title">queryAllStudentCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//设置 redisTemplate 对象 key 的序列化方式</span></span><br><span class="line">        redisTemplate.setKeySerializer(<span class="keyword">new</span> StringRedisSerializer());</span><br><span class="line">        System.out.println(<span class="string">&quot;从redis中获取的&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//从redis缓存中获取总人数</span></span><br><span class="line">        Long allStudentCount = (Long) redisTemplate.opsForValue().get(<span class="string">&quot;allStudentCount&quot;</span>);</span><br><span class="line">        <span class="comment">//如果redis中没有，再去数据库中查询</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == allStudentCount)&#123;</span><br><span class="line">            allStudentCount = studentMapper.selectAllStudentCount();</span><br><span class="line">            redisTemplate.opsForValue().set(<span class="string">&quot;allStudentCount&quot;</span>,allStudentCount,<span class="number">15</span>, TimeUnit.SECONDS);</span><br><span class="line">            System.out.println(<span class="string">&quot;从数据库中查找的&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> allStudentCount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>i&gt; 创建Rediscontroller类进行测试</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedisController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentService studentService;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 请求地址：</span></span><br><span class="line"><span class="comment">     http://localhost:9090/springboot/allStudentCount</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(value = &quot;/springboot/allStudentCount&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">allStudentCount</span><span class="params">(HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        Long allStudentCount = studentService.queryAllStudentCount();</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;学生总人数：&quot;</span> + allStudentCount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>启动运行</p>
<p><font color='red'>报错信息</font></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">There was an unexpected error (type=Internal Server Error, status=500).</span><br><span class="line">Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.227.20:6379</span><br><span class="line">org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 192.168.227.20:6379</span><br></pre></td></tr></table></figure>

<p>解决方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">vim redis.conf</span><br><span class="line"></span><br><span class="line">修改 protected-mode  yes 改为：protected-mode no</span><br><span class="line">注释掉 <span class="comment">#bind 127.0.0.1</span></span><br><span class="line"></span><br><span class="line">保存后，我们需要重新启动redis服务</span><br></pre></td></tr></table></figure>

<p>重新启动运行后</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222206871.png" alt="image-20220222220604945"></p>
<p><strong>控制台</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222211145.png" alt="image-20220222221143435"></p>
<p>出现这种现在的原因是我们给redis设置了ttl过期时间为15秒</p>
<h2 id="4-6-Spring-Boot集成Dubbo"><a href="#4-6-Spring-Boot集成Dubbo" class="headerlink" title="4.6.Spring Boot集成Dubbo"></a>4.6.Spring Boot集成Dubbo</h2><p>阿 里 巴 巴 提 供 了 dubbo 集 成 springBoot 开 源 项 目 ， 可 以 到 GitHub 上<a target="_blank" rel="noopener" href="https://github.com/alibaba/dubbo-spring-boot-starter">https://github.com/alibaba/dubbo-spring-boot-starter</a> 查看入门教程</p>
<h3 id="4-6-1-SpringBoot集成dubbo思路"><a href="#4-6-1-SpringBoot集成dubbo思路" class="headerlink" title="4.6.1.SpringBoot集成dubbo思路"></a>4.6.1.SpringBoot集成dubbo思路</h3><h4 id="1-接口工程"><a href="#1-接口工程" class="headerlink" title="1.接口工程"></a>1.接口工程</h4><p>​    a.存放实体bean和业务接口<br>​    b.maven java工程</p>
<h4 id="2-服务提供者"><a href="#2-服务提供者" class="headerlink" title="2.服务提供者"></a>2.服务提供者</h4><p>​    a.SpringBoot web工程<br>​    b.实现业务接口中的接口</p>
<h4 id="3-服务消费者"><a href="#3-服务消费者" class="headerlink" title="3.服务消费者"></a>3.服务消费者</h4><p>​    a.SpringBoot web工程<br>​    b.处理从浏览器客户端发送的请求<br>​    c.消费服务提供者所提供的服务</p>
<h3 id="4-6-2-基本集成步骤"><a href="#4-6-2-基本集成步骤" class="headerlink" title="4.6.2.基本集成步骤"></a>4.6.2.基本集成步骤</h3><h4 id="1-开发Dubbo服务接口"><a href="#1-开发Dubbo服务接口" class="headerlink" title="1.开发Dubbo服务接口"></a>1.开发Dubbo服务接口</h4><p>springboot-dubbo-interface</p>
<p>按照 Dubbo 官方开发建议，创建一个接口项目，<strong>该项目只定义接口和 model 类</strong></p>
<p><strong>A、创建普通Maven项目，dubbo服务接口工程</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202222233853.png" alt="image-20220222223339218"></p>
<p><strong>B、创建UserService接口</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">say</span><span class="params">(String name)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-开发Dubbo服务提供者"><a href="#2-开发Dubbo服务提供者" class="headerlink" title="2.开发Dubbo服务提供者"></a>2.开发Dubbo服务提供者</h4><p>springboot-dubbo-provider</p>
<p><strong>A、创建SpringBoot 框架的 WEB 项目</strong></p>
<p><strong>B、加入Dubbo集成SpringBoot的起步依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Dubbo 集成 SpringBoot 框架起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>C、由于使用zookeeper作为注册中心，需加入zookeeper的客户端依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Zookeeper 客户端依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>D、<font color='red'>按照需要添加</font>MyBatis和MySQL依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--MyBatis 集成 SpringBoot 框架起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--MySQL 数据库驱动--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.49<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>E、加入之间所创建的Maven工程Dubbo接口依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Dubbo 接口工程--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.abc.springboot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>017-springboot-dubbo-interface<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>F、修改SpringBoot的核心配置文件</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置内嵌 Tomcat 端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">8090</span></span><br><span class="line"><span class="comment">#设置上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#配置 dubbo 的服务提供者信息</span></span><br><span class="line"><span class="comment">#服务提供者应用名称(必须写，且不能重复)</span></span><br><span class="line"><span class="meta">spring.application.name</span>=<span class="string">springboot-dubbo-provider</span></span><br><span class="line"><span class="comment">#设置当前工程为服务提供者</span></span><br><span class="line"><span class="meta">spring.dubbo.server</span>=<span class="string">true</span></span><br><span class="line"><span class="comment">#设置注册中心</span></span><br><span class="line"><span class="meta">spring.dubbo.registry</span>=<span class="string">zookeeper://localhost:2181</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：Dubbo 的注解都是自定义的注解，由我们添加的 Dubbo 依赖中的类进行处理编写dubbo配置是没有提示的</strong></p>
<h4 id="3-开发Dubbo服务消费者"><a href="#3-开发Dubbo服务消费者" class="headerlink" title="3.开发Dubbo服务消费者"></a>3.开发Dubbo服务消费者</h4><p>springboot-dubbo-consumer</p>
<p>A、加入Dubbo集成SpringBoot框架的起步依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Dubbo 集成 SpringBoot 框架起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>B、由于使用zookeeper作为注册中心，需加入zookeeper客户端依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Zookeeper 客户端依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>C、加入Dubbo接口依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--dubbo 接口依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.atyuan<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>017-springboot-dubbo-interface<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>D、SpringBoot的核心配置</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置内嵌 Tomcat 端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">8080</span></span><br><span class="line"><span class="comment">#设置上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#设置 dubbo 配置</span></span><br><span class="line"><span class="comment">#设置服务消费者名称</span></span><br><span class="line"><span class="meta">spring.application.name</span>=<span class="string">springboot-dubbo-consumer</span></span><br><span class="line"><span class="comment">#配置 dubbo 注册中心</span></span><br><span class="line"><span class="meta">spring.dubbo.registry</span>=<span class="string">zookeeper://localhost:2181</span></span><br></pre></td></tr></table></figure>

<p>E、编写Controller类，调用远程的Dubbo服务</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Reference(interfaceClass = UserService.class,version = &quot;1.0.0&quot;,check = false)</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/say&quot;)</span></span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">say</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userService.say(<span class="string">&quot;dubbo&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-在提供者和消费者的主启动类上开启Dubbo注解支持"><a href="#4-在提供者和消费者的主启动类上开启Dubbo注解支持" class="headerlink" title="4.在提供者和消费者的主启动类上开启Dubbo注解支持"></a>4.在提供者和消费者的主启动类上开启Dubbo注解支持</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableDubboConfiguration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootDubboConsumerApplication</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-启动zookeeper服务"><a href="#5-启动zookeeper服务" class="headerlink" title="5.启动zookeeper服务"></a>5.启动zookeeper服务</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">windows：启动zookeeper.cmd</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202230936005.png" alt="image-20220223093648855"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">linux：./zkServer.sh start</span><br></pre></td></tr></table></figure>

<p>先启动服务提供者的主启动类，再启动服务消费者的主启动类</p>
<p>运行成功</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202230935021.png" alt="image-20220223093502105"></p>
<h2 id="4-7-SpringBoot集成SSM，redis，mybatis，dubbo，zookeeper，jsp"><a href="#4-7-SpringBoot集成SSM，redis，mybatis，dubbo，zookeeper，jsp" class="headerlink" title="4.7.SpringBoot集成SSM，redis，mybatis，dubbo，zookeeper，jsp"></a>4.7.SpringBoot集成SSM，redis，mybatis，dubbo，zookeeper，jsp</h2><h3 id="4-7-1-思路"><a href="#4-7-1-思路" class="headerlink" title="4.7.1.思路"></a>4.7.1.思路</h3><ul>
<li>接口工程：存放实体bean和业务接口</li>
<li>服务提供者：它是一个SpringBoot框架web项目，集成MyBatis，Redis<ul>
<li>添加依赖：MyBatis依赖，MySQL驱动依赖，Dubbo依赖，zookeeper依赖，Redis依赖，接口工程依赖</li>
<li>配置SpringBoot核心配置文件<ul>
<li>配置连接数据库</li>
<li>配置连接redis</li>
<li>配置dubbo</li>
</ul>
</li>
</ul>
</li>
<li>服务消费者：它是一个SpringBoot框架web项目，集成JSP，dubbo<ul>
<li>添加依赖：dubbo集成springboot起步依赖，zookeeper客户端依赖，解析JSP页面的依赖，接口工程依赖</li>
<li>配置SpringBoot核心配置文件<ul>
<li>配置视图解析器</li>
<li>配置Dubbo</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="4-7-2-步骤"><a href="#4-7-2-步骤" class="headerlink" title="4.7.2.步骤"></a>4.7.2.步骤</h3><h4 id="1-开发Dubbo服务接口-1"><a href="#1-开发Dubbo服务接口-1" class="headerlink" title="1.开发Dubbo服务接口"></a>1.开发Dubbo服务接口</h4><p>springboot-ssm-dubbo-interface</p>
<blockquote>
<p>编写StudentService接口</p>
</blockquote>
<p>创建com.atyuan.service软件包，如何创建接口StudentService</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="function">Long <span class="title">queryAllStudentCount</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-开发Dubbo服务提供者-1"><a href="#2-开发Dubbo服务提供者-1" class="headerlink" title="2.开发Dubbo服务提供者"></a>2.开发Dubbo服务提供者</h4><p>（1）导入Mybatis逆向工程配置文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">generatorConfiguration</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">generatorConfiguration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 指定连接数据库的 JDBC 驱动包所在位置，指定到你本机的完整路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">classPathEntry</span> <span class="attr">location</span>=<span class="string">&quot;E:\Maven\apache-maven-3.8.2-bin\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 配置 table 表信息内容体，targetRuntime 指定采用 MyBatis3 的版本 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context</span> <span class="attr">id</span>=<span class="string">&quot;tables&quot;</span> <span class="attr">targetRuntime</span>=<span class="string">&quot;MyBatis3&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 抑制生成注释，由于生成的注释都是英文的，可以不让它生成 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suppressAllComments&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置数据库连接信息 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbcConnection</span> <span class="attr">driverClass</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">connectionURL</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/springboot&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">userId</span>=<span class="string">&quot;root&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jdbcConnection</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 model 类，targetPackage 指定 model 类的包名， targetProject 指定</span></span><br><span class="line"><span class="comment">       生成的 model 放在 eclipse 的哪个工程下面--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaModelGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.model&quot;</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">targetProject</span>=<span class="string">&quot;F:\SpringBoot-DataAndCode\IDEA-springboot-projects\springboot-ssm-dubbo-interface\src\main\java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;trimStrings&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaModelGenerator</span>&gt;</span>a</span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper.xml 文件，targetPackage 指定 mapper.xml 文件的</span></span><br><span class="line"><span class="comment">       包名， targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sqlMapGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span></span></span><br><span class="line"><span class="tag">                         <span class="attr">targetProject</span>=<span class="string">&quot;src\main\java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sqlMapGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包</span></span><br><span class="line"><span class="comment">       名， targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaClientGenerator</span> <span class="attr">type</span>=<span class="string">&quot;XMLMAPPER&quot;</span></span></span><br><span class="line"><span class="tag">                             <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaClientGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 数据库表名及对应的 Java 模型类名 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">&quot;t_student&quot;</span> <span class="attr">domainObjectName</span>=<span class="string">&quot;Student&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableCountByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableUpdateByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableDeleteByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">enableSelectByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">selectByExampleQueryId</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">generatorConfiguration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><font color='red'>注意：因为接口工程是存放实体类和接口的，所以逆向工程生成实体类的位置应该是接口工程的目录下，而不是当前目录，且目录中最好不要带有中文</font></p>
<p>（2）导入依赖及相关插件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--        Dubbo 集成 Springboot的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--zookeeper 注册中心--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        接口工程--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.atyuan<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springboot-ssm-dubbo-interface<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        Mybatis 集成Springboot起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        mysql 驱动依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.49<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 加载 spring boot redis 包 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--mybatis 代码自动生成插件--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.generator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-generator-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--配置文件的位置--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">configurationFile</span>&gt;</span>GeneratorMapper.xml<span class="tag">&lt;/<span class="name">configurationFile</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">verbose</span>&gt;</span>true<span class="tag">&lt;/<span class="name">verbose</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">overwrite</span>&gt;</span>true<span class="tag">&lt;/<span class="name">overwrite</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>（3）在右边的maven库中启动插件中的mybatis-generator的generate</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202231956864.png" alt="image-20220223195619777"></p>
<p>（4）配置核心配置文件</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">server.port</span>=<span class="string">9090</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/</span></span><br><span class="line"><span class="comment">#数据库的相关配置</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://localhost:3306/springboot?useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">123456</span></span><br><span class="line"><span class="comment">#redis的相关配置</span></span><br><span class="line"><span class="meta">spring.redis.host</span>=<span class="string">192.168.227.20</span></span><br><span class="line"><span class="meta">spring.redis.port</span>=<span class="string">6379</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#配置 dubbo 的服务提供者信息</span></span><br><span class="line"><span class="comment">#服务提供者应用名称(必须写，且不能重复)</span></span><br><span class="line"><span class="meta">spring.application.name</span>=<span class="string">springboot-ssm-dubbo-provider</span></span><br><span class="line"><span class="comment">#设置当前工程为服务提供者</span></span><br><span class="line"><span class="meta">spring.dubbo.server</span>=<span class="string">true</span></span><br><span class="line"><span class="comment">#设置注册中心</span></span><br><span class="line"><span class="meta">spring.dubbo.registry</span>=<span class="string">zookeeper://192.168.227.20:2181</span></span><br></pre></td></tr></table></figure>

<p>（5）在逆向工程所创建的StudentMapper以及对应的映射文件中配置查询学生总人数的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Long <span class="title">selectAllStudentCount</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectAllStudentCount&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.lang.Long&quot;</span>&gt;</span></span><br><span class="line">    select count(*) from t_student</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>(6)编写StudentServiceImpl实现类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span>  <span class="comment">//注入到IOC容器中，StudentService才能引用</span></span><br><span class="line"><span class="comment">//暴露服务</span></span><br><span class="line"><span class="meta">@Service(interfaceClass = StudentService.class,version = &quot;1.0.0&quot;,timeout = 15000)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentServiceImpl</span> <span class="keyword">implements</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentMapper studentMapper;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    RedisTemplate&lt;Object,Object&gt; redisTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Long <span class="title">queryAllStudentCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line">        Long allStudentCount = (Long) redisTemplate.opsForValue().get(<span class="string">&quot;allStudentCount&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">null</span> == allStudentCount)&#123;</span><br><span class="line">            allStudentCount = studentMapper.selectAllStudentCount();</span><br><span class="line">            redisTemplate.opsForValue().set(<span class="string">&quot;allStudentCount&quot;</span>,allStudentCount,<span class="number">10</span>, TimeUnit.SECONDS);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> allStudentCount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>（7）主启动类上标记开启Dubbo注解支持</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@EnableDubboConfiguration</span></span><br></pre></td></tr></table></figure>

<p><font color='red'>注意</font>：springboot-ssm-dubbo-provider<strong>在使用逆向工程时不要导入接口工程的依赖</strong>，等生成代码后，再添加接口工程依赖，否则会报以下错误：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[ERROR] Failed to execute goal on project springboot-ssm-dubbo-provider: Could not resolve dependencies <span class="keyword">for</span> project com.atyuan:springboot-ssm-dubbo-provider:jar:0.0.1-SNAPSHOT: Could not find artifact com.atyuan:springboot-ssm-dubbo-interface:jar:1.0-SNAPSHOT -&gt; [Help 1]</span><br></pre></td></tr></table></figure>

<h4 id="3-开发Dubbo服务消费者-1"><a href="#3-开发Dubbo服务消费者-1" class="headerlink" title="3.开发Dubbo服务消费者"></a>3.开发Dubbo服务消费者</h4><p>（1）导入相关依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--        Dubbo 集成 springboot的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--zookeeper 注册中心--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--引入 Spring Boot 内嵌的 Tomcat 对 JSP 的解析包，不加解析不了 jsp 页面--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果只是使用 JSP 页面，可以只添加该依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.embed<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat-embed-jasper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--        接口工程依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.atyuan<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springboot-ssm-dubbo-interface<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意：SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources 目录下才能访问，否则访问不到。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--源文件位置--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--指定编译到 META-INF/resources，该目录不能随便写--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--指定要把哪些文件编译进去，**表示 webapp 目录及子目录，*.*表示所有文件--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>（2）配置核心配置文件</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">server.port</span>=<span class="string">8080</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#配置 dubbo 的服务消费者信息</span></span><br><span class="line"><span class="comment">#服务消费者应用名称(必须写，且不能重复)</span></span><br><span class="line"><span class="meta">spring.application.name</span>=<span class="string">springboot-ssm-dubbo-consumer</span></span><br><span class="line"><span class="comment">#设置注册中心</span></span><br><span class="line"><span class="meta">spring.dubbo.registry</span>=<span class="string">zookeeper://192.168.227.20:2181</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#配置视图解析器</span></span><br><span class="line"><span class="meta">spring.mvc.view.prefix</span>=<span class="string">/</span></span><br><span class="line"><span class="meta">spring.mvc.view.suffix</span>=<span class="string">.jsp</span></span><br></pre></td></tr></table></figure>

<p>（3）服务消费者集成jsp</p>
<p>在main目录下新建webapp目录，然后打开项目结构，设置web资源目录</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202231517088.png" alt="image-20220223151745948"></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202231519524.png" alt="image-20220223151929072"></p>
<p>（3）编写Controller类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Reference(interfaceClass = StudentService.class,version = &quot;1.0.0&quot;,check = false)</span></span><br><span class="line">    <span class="keyword">private</span> StudentService studentService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/count&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">queryAll</span><span class="params">(HttpServletRequest request)</span></span>&#123;</span><br><span class="line">        Long count = studentService.queryAllStudentCount();</span><br><span class="line">        request.setAttribute(<span class="string">&quot;data&quot;</span>,count);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（4）在主启动类上开启Dubbo注解支持</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@EnableDubboConfiguration</span></span><br></pre></td></tr></table></figure>

<h4 id="4-运行测试"><a href="#4-运行测试" class="headerlink" title="4.运行测试"></a>4.运行测试</h4><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202231508790.png" alt="image-20220223150828276"></p>
<h2 id="五、SpringBoot非web应用程序"><a href="#五、SpringBoot非web应用程序" class="headerlink" title="五、SpringBoot非web应用程序"></a>五、SpringBoot非web应用程序</h2><p>在 Spring Boot 框架中，要创建一个非 Web 应用程序（纯 Java 程序），有两种方式</p>
<h2 id="5-1-方式一：直接在-main-方法中，根据-SpringApplication-run-方法获取返回的Spring容器对象，再获取业务bean-进行调用"><a href="#5-1-方式一：直接在-main-方法中，根据-SpringApplication-run-方法获取返回的Spring容器对象，再获取业务bean-进行调用" class="headerlink" title="5.1.方式一：直接在 main 方法中，根据 SpringApplication.run()方法获取返回的Spring容器对象，再获取业务bean 进行调用"></a>5.1.方式一：直接在 main 方法中，根据 SpringApplication.run()方法获取返回的Spring容器对象，再获取业务bean 进行调用</h2><h3 id="5-1-1-创建普通的SpringBoot项目，不要添加web模块支持"><a href="#5-1-1-创建普通的SpringBoot项目，不要添加web模块支持" class="headerlink" title="5.1.1.创建普通的SpringBoot项目，不要添加web模块支持"></a>5.1.1.创建普通的SpringBoot项目，不要添加web模块支持</h3><p>springboot-java-01</p>
<h3 id="5-1-2-创建一个UserService接口及其实现类"><a href="#5-1-2-创建一个UserService接口及其实现类" class="headerlink" title="5.1.2.创建一个UserService接口及其实现类"></a>5.1.2.创建一个UserService接口及其实现类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">sayHello</span><span class="params">(String msg)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String msg)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Hello:&quot;</span>+msg;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：UserServiceImpl需要注入到IOC容器中，不然无法通过Bean查找</p>
<h3 id="5-1-3-修改主启动类"><a href="#5-1-3-修改主启动类" class="headerlink" title="5.1.3.修改主启动类"></a>5.1.3.修改主启动类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * SpringBoot 程序启动后，返回值是 ConfigurableApplicationContext，它也是一个Spring 容器对象</span></span><br><span class="line"><span class="comment">         * 它其它相当于原来 Spring 中启动容器 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(&quot;&quot;);</span></span><br><span class="line"><span class="comment"> 		*/</span></span><br><span class="line">        <span class="comment">//获取 SpringBoot 程序启动后的 Spring 容器</span></span><br><span class="line">        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);</span><br><span class="line">        <span class="comment">//从 Spring 容器中获取指定 bean 的对象</span></span><br><span class="line">        UserService userService = (UserService) </span><br><span class="line">            context.getBean(<span class="string">&quot;userServiceImpl&quot;</span>);</span><br><span class="line">        <span class="comment">//调用业务 bean 的方法</span></span><br><span class="line">        String sayHello = userService.sayHello(<span class="string">&quot;Tomcat&quot;</span>);</span><br><span class="line">        System.out.println(sayHello);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-1-4-测试结果"><a href="#5-1-4-测试结果" class="headerlink" title="5.1.4.测试结果"></a>5.1.4.测试结果</h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202232038738.png" alt="image-20220223203833699"></p>
<h2 id="5-2-方式二：Spring-boot-的入口类实现CommandLineRunner-接口"><a href="#5-2-方式二：Spring-boot-的入口类实现CommandLineRunner-接口" class="headerlink" title="5.2.方式二：Spring boot 的入口类实现CommandLineRunner 接口"></a>5.2.方式二：Spring boot 的入口类实现CommandLineRunner 接口</h2><h3 id="5-2-1-创建一个普通的SpringBoot工程"><a href="#5-2-1-创建一个普通的SpringBoot工程" class="headerlink" title="5.2.1.创建一个普通的SpringBoot工程"></a>5.2.1.创建一个普通的SpringBoot工程</h3><p>springboot-java-02</p>
<h3 id="5-2-2-创建一个UserService接口及其实现类"><a href="#5-2-2-创建一个UserService接口及其实现类" class="headerlink" title="5.2.2.创建一个UserService接口及其实现类"></a>5.2.2.创建一个UserService接口及其实现类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function">String <span class="title">sayHello</span><span class="params">(String msg)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String msg)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Hello:&quot;</span>+msg;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：UserServiceImpl需要注入到IOC容器中，不然无法通过Bean查找</p>
<h3 id="5-2-3-修改主启动类"><a href="#5-2-3-修改主启动类" class="headerlink" title="5.2.3.修改主启动类"></a>5.2.3.修改主启动类</h3><p>主启动类实现 CommandLineRunner 接口并重写run（）方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootJava02Application</span> <span class="keyword">implements</span> <span class="title">CommandLineRunner</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//SpringBoot启动程序，会初始化Spring容器</span></span><br><span class="line">        SpringApplication.run(SpringbootJava02Application.class, args);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(String... args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        String say = userService.sayHello(<span class="string">&quot;World&quot;</span>);</span><br><span class="line">        System.out.println(say);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-2-4-测试结果"><a href="#5-2-4-测试结果" class="headerlink" title="5.2.4.测试结果"></a>5.2.4.测试结果</h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202232109860.png" alt="image-20220223210914397"></p>
<h2 id="5-3-SpringBoot启动时的LOGO"><a href="#5-3-SpringBoot启动时的LOGO" class="headerlink" title="5.3.SpringBoot启动时的LOGO"></a>5.3.SpringBoot启动时的LOGO</h2><h3 id="5-3-1-关闭SpringBoot-启动时的LOGO"><a href="#5-3-1-关闭SpringBoot-启动时的LOGO" class="headerlink" title="5.3.1.关闭SpringBoot 启动时的LOGO"></a>5.3.1.关闭SpringBoot 启动时的LOGO</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootLogoApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication springApplication = <span class="keyword">new</span> SpringApplication(SpringbootLogoApplication.class);</span><br><span class="line">        <span class="comment">//关闭启动 logo 的输出</span></span><br><span class="line">        springApplication.setBannerMode(Banner.Mode.OFF);</span><br><span class="line">        springApplication.run(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-3-2-修改SpringBoot启动时的LOGO"><a href="#5-3-2-修改SpringBoot启动时的LOGO" class="headerlink" title="5.3.2.修改SpringBoot启动时的LOGO"></a>5.3.2.修改SpringBoot启动时的LOGO</h3><p>在src\main\resource目录下放入 banner.txt 文件，该文件名字不能随意，文件中的内容就是要输出的 logo ； 可 以 利 用 网 站 生 成 图 标 ： <a target="_blank" rel="noopener" href="https://www.bootschool.net/ascii">https://www.bootschool.net/ascii</a> 或 者<a target="_blank" rel="noopener" href="http://patorjk.com/software/taag/%EF%BC%8C%E5%B0%86%E7%94%9F%E6%88%90%E5%A5%BD%E7%9A%84%E5%9B%BE%E6%A0%87%E6%96%87%E5%AD%97%E7%B2%98%E8%B4%B4%E5%88%B0">http://patorjk.com/software/taag/，将生成好的图标文字粘贴到</a> banner.txt 文件中，然后将关闭 logo 输出的语句注释，启动看效果</p>
<h1 id="六、SpringBoot使用拦截器"><a href="#六、SpringBoot使用拦截器" class="headerlink" title="六、SpringBoot使用拦截器"></a>六、SpringBoot使用拦截器</h1><h2 id="6-1-回顾SpringMVC使用拦截器步骤"><a href="#6-1-回顾SpringMVC使用拦截器步骤" class="headerlink" title="6.1.回顾SpringMVC使用拦截器步骤"></a>6.1.回顾SpringMVC使用拦截器步骤</h2><h3 id="6-1-1-配置文件版"><a href="#6-1-1-配置文件版" class="headerlink" title="6.1.1.配置文件版"></a>6.1.1.配置文件版</h3><p>自定义拦截器类，实现HandlerInterceptor接口</p>
<p>注册拦截器类</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mvc:interceptors</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">mvc:mapper</span> <span class="attr">path</span>=<span class="string">&quot;/&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">mvc:exclude-mapping</span> <span class="attr">path</span>=<span class="string">&quot;/say&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">ref</span> <span class="attr">id</span>=<span class="string">&quot;myInterceptor&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mvc:interceptors</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="6-2-2-注解版"><a href="#6-2-2-注解版" class="headerlink" title="6.2.2.注解版"></a>6.2.2.注解版</h3><p>注册拦截器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addInterceptors</span><span class="params">(InterceptorRegistry registry)</span> </span>&#123;</span><br><span class="line">        registry.addInterceptor(<span class="keyword">new</span> MyInterceptor())</span><br><span class="line">                .addPathPatterns(<span class="string">&quot;/&quot;</span>).excludePathPatterns(<span class="string">&quot;/say&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-3-SpringBoot使用拦截器"><a href="#6-3-SpringBoot使用拦截器" class="headerlink" title="6.3.SpringBoot使用拦截器"></a>6.3.SpringBoot使用拦截器</h2><p>springboot使用拦截器的方法与SpringMVC使用注解注册拦截器的方法一样，这部分忘了可参考<a target="_blank" rel="noopener" href="https://rmbyuan.gitee.io/2022/02/14/SpringMVC%E7%AC%94%E8%AE%B0/">SpringMVC的笔记</a></p>
<h1 id="七、SpringBoot中使用Servlet（了解）"><a href="#七、SpringBoot中使用Servlet（了解）" class="headerlink" title="七、SpringBoot中使用Servlet（了解）"></a>七、SpringBoot中使用Servlet（了解）</h1><h2 id="7-1-方式一：通过注解扫描方式实现"><a href="#7-1-方式一：通过注解扫描方式实现" class="headerlink" title="7.1.方式一：通过注解扫描方式实现"></a>7.1.方式一：通过注解扫描方式实现</h2><p>项目：springboot-servlet</p>
<h3 id="7-1-1-通过注解方式创建一个Servlet"><a href="#7-1-1-通过注解方式创建一个Servlet" class="headerlink" title="7.1.1.通过注解方式创建一个Servlet"></a>7.1.1.通过注解方式创建一个Servlet</h3><p>创建MyServlet.class</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@WebServlet(urlPatterns = &quot;/myServlet&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        resp.getWriter().print(<span class="string">&quot;My SpringBoot Servlet&quot;</span>);</span><br><span class="line">        resp.getWriter().flush();</span><br><span class="line">        resp.getWriter().close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-1-2-在启动类上标记-ServletComponentScan"><a href="#7-1-2-在启动类上标记-ServletComponentScan" class="headerlink" title="7.1.2.在启动类上标记@ServletComponentScan"></a>7.1.2.在启动类上标记@ServletComponentScan</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ServletComponentScan(&quot;com.atyuan.servlet&quot;)</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202232201147.png" alt="image-20220223220100693"></p>
<h2 id="7-2-方式二：通过SpringBoot的配置类实现（组件注册）"><a href="#7-2-方式二：通过SpringBoot的配置类实现（组件注册）" class="headerlink" title="7.2.方式二：通过SpringBoot的配置类实现（组件注册）"></a>7.2.方式二：通过SpringBoot的配置类实现（组件注册）</h2><h3 id="7-2-1-创建一个普通的Servlet继承类"><a href="#7-2-1-创建一个普通的Servlet继承类" class="headerlink" title="7.2.1.创建一个普通的Servlet继承类"></a>7.2.1.创建一个普通的Servlet继承类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        resp.getWriter().print(<span class="string">&quot;My SpringBoot Servlet&quot;</span>);</span><br><span class="line">        resp.getWriter().flush();</span><br><span class="line">        resp.getWriter().close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-2-2编写一个SpringBoot的配置类，在该类中注册Servlet"><a href="#7-2-2编写一个SpringBoot的配置类，在该类中注册Servlet" class="headerlink" title="7.2.2编写一个SpringBoot的配置类，在该类中注册Servlet"></a>7.2.2编写一个SpringBoot的配置类，在该类中注册Servlet</h3><p>创建Servlet配置类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span> <span class="comment">//将此类作为配置类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServletConfig</span> </span>&#123;</span><br><span class="line">    <span class="comment">//@Bean 是一个方法级别上的注解，主要用在配置类里</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 相当于一个&lt;beans&gt;</span></span><br><span class="line"><span class="comment"> * &lt;bean id=&quot;&quot; class=&quot;&quot;/&gt;</span></span><br><span class="line"><span class="comment"> * &lt;/beans&gt;</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">myServletRegistrationBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//将自定义 servlet 注册到注册 Servlet 类中，并指定访问路径</span></span><br><span class="line">        ServletRegistrationBean servletRegistrationBean = <span class="keyword">new</span> </span><br><span class="line">            ServletRegistrationBean(<span class="keyword">new</span> MyServlet(), <span class="string">&quot;/springboot/myServlet&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> servletRegistrationBean;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="八、SpringBoot中使用Filter（了解）"><a href="#八、SpringBoot中使用Filter（了解）" class="headerlink" title="八、SpringBoot中使用Filter（了解）"></a>八、SpringBoot中使用Filter（了解）</h1><p>用法与servlet类似</p>
<h2 id="8-1-方式一：通过注解方式实现"><a href="#8-1-方式一：通过注解方式实现" class="headerlink" title="8.1.方式一：通过注解方式实现"></a>8.1.方式一：通过注解方式实现</h2><h3 id="8-1-1-创建就一个自定义的Filter类"><a href="#8-1-1-创建就一个自定义的Filter类" class="headerlink" title="8.1.1.创建就一个自定义的Filter类"></a>8.1.1.创建就一个自定义的Filter类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@WebFilter(urlPatterns = &quot;/myServlet&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFilter</span> <span class="keyword">implements</span> <span class="title">Filter</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter初始化成功....&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;-------------您已进入过滤器---------------&quot;</span>);</span><br><span class="line">        servletRequest.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        servletResponse.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest,servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter已销毁....&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-1-2-在启动类上使用-ServletComponentScan注解"><a href="#8-1-2-在启动类上使用-ServletComponentScan注解" class="headerlink" title="8.1.2.在启动类上使用@ServletComponentScan注解"></a>8.1.2.在启动类上使用@ServletComponentScan注解</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@ServletComponentScan(basePackages = &quot;com.atyuan.filter&quot;)</span><br></pre></td></tr></table></figure>

<h3 id="8-1-3-在浏览器中发送请求-myServlet"><a href="#8-1-3-在浏览器中发送请求-myServlet" class="headerlink" title="8.1.3.在浏览器中发送请求/myServlet"></a>8.1.3.在浏览器中发送请求/myServlet</h3><p>控制台看到请求被过滤了</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202232223687.png" alt="image-20220223222316538"></p>
<h2 id="8-2-方式二：通过配置类方式实现"><a href="#8-2-方式二：通过配置类方式实现" class="headerlink" title="8.2.方式二：通过配置类方式实现"></a>8.2.方式二：通过配置类方式实现</h2><h3 id="8-2-1-创建一个自定义的Filter类"><a href="#8-2-1-创建一个自定义的Filter类" class="headerlink" title="8.2.1.创建一个自定义的Filter类"></a>8.2.1.创建一个自定义的Filter类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFilter</span> <span class="keyword">implements</span> <span class="title">Filter</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter初始化成功....&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;-------------您已进入过滤器---------------&quot;</span>);</span><br><span class="line">        servletRequest.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        servletResponse.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest,servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter已销毁....&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-2-2-创建一个配置类"><a href="#8-2-2-创建一个配置类" class="headerlink" title="8.2.2.创建一个配置类"></a>8.2.2.创建一个配置类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FilterConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> FilterRegistrationBean <span class="title">filterRegistrationBean</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//注册过滤器</span></span><br><span class="line">        FilterRegistrationBean&lt;Filter&gt; filterRegistrationBean = <span class="keyword">new</span> FilterRegistrationBean&lt;&gt;(<span class="keyword">new</span> MyFilter());</span><br><span class="line">        <span class="comment">//设置需要过滤的路径</span></span><br><span class="line">        filterRegistrationBean.addUrlPatterns(<span class="string">&quot;/*&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> filterRegistrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="九、SpringBoot项目配置字符编码"><a href="#九、SpringBoot项目配置字符编码" class="headerlink" title="九、SpringBoot项目配置字符编码"></a>九、SpringBoot项目配置字符编码</h1><h2 id="9-1-方式一：使用窗体的Spring提供的字符编码过滤器"><a href="#9-1-方式一：使用窗体的Spring提供的字符编码过滤器" class="headerlink" title="9.1.方式一：使用窗体的Spring提供的字符编码过滤器"></a>9.1.方式一：使用窗体的Spring提供的字符编码过滤器</h2><h3 id="9-1-1-创建一个Servlet"><a href="#9-1-1-创建一个Servlet" class="headerlink" title="9.1.1.创建一个Servlet"></a>9.1.1.创建一个Servlet</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        resp.getWriter().print(<span class="string">&quot;Hello World，世界您好！&quot;</span>);</span><br><span class="line">        <span class="comment">//设置浏览器编码格式</span></span><br><span class="line">        resp.setContentType(<span class="string">&quot;text/html;character=utf-8&quot;</span>);</span><br><span class="line">        resp.getWriter().flush();</span><br><span class="line">        resp.getWriter().close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-1-2-配置WebConfig"><a href="#9-1-2-配置WebConfig" class="headerlink" title="9.1.2.配置WebConfig"></a>9.1.2.配置WebConfig</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">myServletRegistration</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ServletRegistrationBean servletRegistrationBean =</span><br><span class="line">                <span class="keyword">new</span> ServletRegistrationBean(<span class="keyword">new</span> MyServlet(),</span><br><span class="line">                        <span class="string">&quot;/myServlet&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> servletRegistrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> FilterRegistrationBean <span class="title">characterFilterRegistration</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//设置字符编码过滤器</span></span><br><span class="line">        <span class="comment">//CharacterEncoding 是由 Spring 提供的一个字符编码过滤器，之前是配置在web.xml 文件中</span></span><br><span class="line">        CharacterEncodingFilter characterEncodingFilter = <span class="keyword">new</span> CharacterEncodingFilter();</span><br><span class="line">        <span class="comment">//强制使用指定字符编码</span></span><br><span class="line">        characterEncodingFilter.setForceEncoding(<span class="keyword">true</span>);</span><br><span class="line">        <span class="comment">//设置指定字符编码</span></span><br><span class="line">        characterEncodingFilter.setEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        <span class="comment">//创建过滤器注册 bean</span></span><br><span class="line">        FilterRegistrationBean filterRegistrationBean = <span class="keyword">new</span> FilterRegistrationBean();</span><br><span class="line">        <span class="comment">//设置字符编码过滤器</span></span><br><span class="line">        filterRegistrationBean.setFilter(characterEncodingFilter);</span><br><span class="line">        <span class="comment">//设置字符编码过滤器路径</span></span><br><span class="line">        filterRegistrationBean.addUrlPatterns(<span class="string">&quot;/*&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> filterRegistrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-1-3-关闭SpringBoot的http字符编码支持"><a href="#9-1-3-关闭SpringBoot的http字符编码支持" class="headerlink" title="9.1.3.关闭SpringBoot的http字符编码支持"></a>9.1.3.关闭SpringBoot的http字符编码支持</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#关闭 springboot 的 http 字符编码支持</span></span><br><span class="line"><span class="comment">#只有关闭该选项后，spring 字符编码过滤器才生效</span></span><br><span class="line"><span class="meta">server.servlet.encoding.enabled</span>=<span class="string">false</span></span><br></pre></td></tr></table></figure>

<h3 id="9-1-4-测试"><a href="#9-1-4-测试" class="headerlink" title="9.1.4.测试"></a>9.1.4.测试</h3><p>无字符编码过滤器</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241329658.png" alt="image-20220224132944403"></p>
<p>使用字符编码过滤器</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202232251151.png" alt="image-20220223225131811"></p>
<p><strong>在servlet中添加response.setContextType（“text/html;charset=utf-8”）指定浏览器编码方式</strong></p>
<h2 id="9-2-方式二：在application-properties中配置字符编码（推荐）"><a href="#9-2-方式二：在application-properties中配置字符编码（推荐）" class="headerlink" title="9.2.方式二：在application.properties中配置字符编码（推荐）"></a>9.2.方式二：在application.properties中配置字符编码（推荐）</h2><h3 id="9-2-1-SpringBoot核心配置文件添加字符编码设置"><a href="#9-2-1-SpringBoot核心配置文件添加字符编码设置" class="headerlink" title="9.2.1.SpringBoot核心配置文件添加字符编码设置"></a>9.2.1.SpringBoot核心配置文件添加字符编码设置</h3><p>从springboot1.4.2之后开始新增的一种字符编码设置</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置请求响应的字符编码</span></span><br><span class="line"><span class="meta">server.servlet.encoding.enabled</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">server.servlet.encoding.force</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">server.servlet.encoding.charset</span>=<span class="string">UTF-8</span></span><br></pre></td></tr></table></figure>

<h3 id="9-2-2-自定义Servlet"><a href="#9-2-2-自定义Servlet" class="headerlink" title="9.2.2.自定义Servlet"></a>9.2.2.自定义Servlet</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        resp.getWriter().print(<span class="string">&quot;Hello World，世界您好！&quot;</span>);</span><br><span class="line">        <span class="comment">//设置浏览器编码格式</span></span><br><span class="line">        resp.setContentType(<span class="string">&quot;text/html;character=utf-8&quot;</span>);</span><br><span class="line">        resp.getWriter().flush();</span><br><span class="line">        resp.getWriter().close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>&#123;</span><br><span class="line">        doGet(req,resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-2-3-创建配置类，注册Servlet映射"><a href="#9-2-3-创建配置类，注册Servlet映射" class="headerlink" title="9.2.3.创建配置类，注册Servlet映射"></a>9.2.3.创建配置类，注册Servlet映射</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServletConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">myServletRegistration</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ServletRegistrationBean servletRegistrationBean = <span class="keyword">new</span> ServletRegistrationBean(<span class="keyword">new</span> MyServlet(),<span class="string">&quot;/myservlet&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> servletRegistrationBean;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-2-3-测试"><a href="#9-2-3-测试" class="headerlink" title="9.2.3.测试"></a>9.2.3.测试</h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241336458.png" alt="image-20220224133651502"></p>
<h1 id="十、SpringBoot打包与部署"><a href="#十、SpringBoot打包与部署" class="headerlink" title="十、SpringBoot打包与部署"></a>十、SpringBoot打包与部署</h1><h2 id="10-1-SpringBoot程序war包部署"><a href="#10-1-SpringBoot程序war包部署" class="headerlink" title="10.1.SpringBoot程序war包部署"></a>10.1.SpringBoot程序war包部署</h2><h3 id="10-1-1创建SpringBoot-Web项目"><a href="#10-1-1创建SpringBoot-Web项目" class="headerlink" title="10.1.1创建SpringBoot Web项目"></a>10.1.1创建SpringBoot Web项目</h3><h4 id="1-添加SpringBoot解析jsp依赖"><a href="#1-添加SpringBoot解析jsp依赖" class="headerlink" title="1.添加SpringBoot解析jsp依赖"></a>1.添加SpringBoot解析jsp依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--引入 Spring Boot 内嵌的 Tomcat 对 JSP 的解析包，不加解析不了 jsp 页面--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--如果只是使用 JSP 页面，可以只添加该依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.embed<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat-embed-jasper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-在pom-xml文件中配置jsp文件解析目录"><a href="#2-在pom-xml文件中配置jsp文件解析目录" class="headerlink" title="2.在pom.xml文件中配置jsp文件解析目录"></a>2.在pom.xml文件中配置jsp文件解析目录</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment"> SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources 目录下，否则不能访问</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--源文件位置--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--指定编译到 META-INF/resources 目录下，该目录不能随便编写--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--指定包含文件--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="3-创建webapp并指定为web资源文件夹"><a href="#3-创建webapp并指定为web资源文件夹" class="headerlink" title="3.创建webapp并指定为web资源文件夹"></a>3.创建webapp并指定为web资源文件夹</h4><h4 id="4-在application-properties配置文件中配置视图解析器"><a href="#4-在application-properties配置文件中配置视图解析器" class="headerlink" title="4.在application.properties配置文件中配置视图解析器"></a>4.在application.properties配置文件中配置视图解析器</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">spring.mvc.view.prefix</span>=<span class="string">/</span></span><br><span class="line"><span class="meta">spring.mvc.view.suffix</span>=<span class="string">.jsp</span></span><br></pre></td></tr></table></figure>

<h4 id="5-创建IndexController"><a href="#5-创建IndexController" class="headerlink" title="5.创建IndexController"></a>5.创建IndexController</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WarController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/abc&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">        model.addAttribute(<span class="string">&quot;data&quot;</span>,<span class="string">&quot;SpringBoot&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="6-在webapp目录下创建index-jsp页面"><a href="#6-在webapp目录下创建index-jsp页面" class="headerlink" title="6.在webapp目录下创建index.jsp页面"></a>6.在webapp目录下创建index.jsp页面</h4><h3 id="10-1-2-打war包"><a href="#10-1-2-打war包" class="headerlink" title="10.1.2.打war包"></a>10.1.2.打war包</h3><p>1.启动类需继承<strong>SpringBootServletInitializer类</strong>，并重写configure方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> <span class="keyword">extends</span> <span class="title">SpringBootServletInitializer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(Application.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> SpringApplicationBuilder <span class="title">configure</span><span class="params">(SpringApplicationBuilder builder)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//参数为当前 SpringBoot 启动类</span></span><br><span class="line">        <span class="keyword">return</span> builder.sources(Application.class);</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2.在pom.xml中添加（修改）打包方式为war</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">packaging</span>&gt;</span>war<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>3.在pom.xml中配置springboot打包的插件（默认自动加）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SpringBoot 打包插件--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>4.在pom.xml文件中配置将配置文件编译到类路径</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--源文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--目标文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--包含的文件--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--mybatis 的 mapper.xml--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--src/main/resources 下的所有配置文件编译到 classes 下面去--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/resources<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>5.在pom.xml的build标签下通过finalName指定打包的名字</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--指定打war包的名字--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">finalName</span>&gt;</span>springboot<span class="tag">&lt;/<span class="name">finalName</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>6.通过 Maven package命令打 war 包到 target 目录下</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241434506.png" alt="image-20220224143405423"></p>
<h3 id="10-1-3-部署到Tomcat服务器上测试运行"><a href="#10-1-3-部署到Tomcat服务器上测试运行" class="headerlink" title="10.1.3.部署到Tomcat服务器上测试运行"></a>10.1.3.部署到Tomcat服务器上测试运行</h3><p>1.将target目录下生成的war包拷贝到tomcat的webapps目录下，并启动tomcat</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241437387.png" alt="image-20220224143712164"></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241439612.png" alt="image-20220224143859941"></p>
<p>浏览器的访问路径：<a href="http://localhost:端口/war包名称/请求路径">http://localhost:端口/war包名称/请求路径</a></p>
<h2 id="10-2-SpringBoot程序打jar包与运行"><a href="#10-2-SpringBoot程序打jar包与运行" class="headerlink" title="10.2.SpringBoot程序打jar包与运行"></a>10.2.SpringBoot程序打jar包与运行</h2><p>因为 SpringBoot 默认打包方式就是 jar 包,所以我们直接执行 Maven 的 package 命令就行了</p>
<h3 id="10-2-1-在pom-xml文件中添加Tomcat解析jsp依赖"><a href="#10-2-1-在pom-xml文件中添加Tomcat解析jsp依赖" class="headerlink" title="10.2.1.在pom.xml文件中添加Tomcat解析jsp依赖"></a>10.2.1.在pom.xml文件中添加Tomcat解析jsp依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SpringBoot 项目内嵌 tomcat 对 jsp 的解析包--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.tomcat.embed<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>tomcat-embed-jasper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="10-2-2-在pom-xml文件中添加resources配置，以后为了保险起见，在打包的时候，建议把下面的配置都加上"><a href="#10-2-2-在pom-xml文件中添加resources配置，以后为了保险起见，在打包的时候，建议把下面的配置都加上" class="headerlink" title="10.2.2.在pom.xml文件中添加resources配置，以后为了保险起见，在打包的时候，建议把下面的配置都加上"></a>10.2.2.在pom.xml文件中添加resources配置，以后为了保险起见，在打包的时候，建议把下面的配置都加上</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--源文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--目标文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--包含的文件--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--mybatis 的 mapper.xml--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--src/main/resources 下的所有配置文件编译到 classes 下面去--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/resources<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="10-2-3-修改pom-xml文件中打包插件的版本"><a href="#10-2-3-修改pom-xml文件中打包插件的版本" class="headerlink" title="10.2.3.修改pom.xml文件中打包插件的版本"></a>10.2.3.修改pom.xml文件中打包插件的版本</h3><p><strong>默认SpingBoot提供的打包插件版本为2.2.2.RELEASE，这个版本打的jar 包 jsp 不能访问，我们这里修改为1.4.2.RELEASE（其它版本测试都有问题）</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- SpringBoot 提供打包编译插件 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.2.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="10-2-4-修改application-properties配置文件"><a href="#10-2-4-修改application-properties配置文件" class="headerlink" title="10.2.4.修改application.properties配置文件"></a>10.2.4.修改application.properties配置文件</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置内嵌 Tomcat 端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">9090</span></span><br><span class="line"><span class="comment">#设置项目上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/ #配置 jsp 的前/后缀</span></span><br><span class="line"><span class="meta">spring.mvc.view.prefix</span>=<span class="string">/</span></span><br><span class="line"><span class="meta">spring.mvc.view.suffix</span>=<span class="string">.jsp</span></span><br></pre></td></tr></table></figure>

<h3 id="10-2-5-创建controller类"><a href="#10-2-5-创建controller类" class="headerlink" title="10.2.5.创建controller类"></a>10.2.5.创建controller类</h3><h3 id="10-2-6-创建webapp目录并指定为web资源目录"><a href="#10-2-6-创建webapp目录并指定为web资源目录" class="headerlink" title="10.2.6.创建webapp目录并指定为web资源目录"></a>10.2.6.创建webapp目录并指定为web资源目录</h3><h3 id="10-2-7-打包package"><a href="#10-2-7-打包package" class="headerlink" title="10.2.7.打包package"></a>10.2.7.打包package</h3><h3 id="10-2-8-运行方式"><a href="#10-2-8-运行方式" class="headerlink" title="10.2.8.运行方式"></a>10.2.8.运行方式</h3><p>（1）通过java命令执行jar包，相当于启动内嵌tomcat</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">将 target 下的 jar 包拷贝到某一个目录，在该目录下执行 java -jar jar 包名称</span><br></pre></td></tr></table></figure>

<p>（2）在IDEA中右键运行</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202241548471.png" alt="image-20220224154806071"></p>
<h2 id="10-3-SpringBoot部署与运行方式总结"><a href="#10-3-SpringBoot部署与运行方式总结" class="headerlink" title="10.3.SpringBoot部署与运行方式总结"></a>10.3.SpringBoot部署与运行方式总结</h2><p>（1）开发阶段：在IDEA中直接运行SpringBoot程序的main方法</p>
<p>用maven将项目打包，打包方式为jar包</p>
<p>（2）上线部署：将jar包上传到服务器上，如何在服务器中写一个shell脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/sh</span></span><br><span class="line">java -jar xxx.jar</span><br></pre></td></tr></table></figure>

<p>赋权限 chmod 777 run.sh</p>
<p>启动shell脚本： ./run.sh</p>
<p><font color='red'>使用 Spring Boot 的 maven 插件将 Springboot 程序打成 war 包，单独部署在 tomcat 中运行（上线部署常用）</font></p>
<h1 id="十一、SpringBoot集成logback日志"><a href="#十一、SpringBoot集成logback日志" class="headerlink" title="十一、SpringBoot集成logback日志"></a>十一、SpringBoot集成logback日志</h1><h2 id="11-1-导入mybatis相关依赖和mysql驱动"><a href="#11-1-导入mybatis相关依赖和mysql驱动" class="headerlink" title="11.1.导入mybatis相关依赖和mysql驱动"></a>11.1.导入mybatis相关依赖和mysql驱动</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--MyBatis 集成 SpringBoot 框架的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--连接 MySQL 的驱动--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="11-2-编写核心配置文件"><a href="#11-2-编写核心配置文件" class="headerlink" title="11.2.编写核心配置文件"></a>11.2.编写核心配置文件</h2><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置内嵌 Tomcat 端口号</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">9090</span></span><br><span class="line"><span class="comment">#设置项目上下文根</span></span><br><span class="line"><span class="meta">server.servlet.context-path</span>=<span class="string">/ </span></span><br><span class="line"><span class="comment">#配置 jsp 的前/后缀</span></span><br><span class="line"><span class="meta">spring.mvc.view.prefix</span>=<span class="string">/</span></span><br><span class="line"><span class="meta">spring.mvc.view.suffix</span>=<span class="string">.jsp</span></span><br><span class="line"><span class="comment">#配置连接 MySQL 数据库信息</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://localhost:3306/springboot?useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">123456</span></span><br></pre></td></tr></table></figure>

<h2 id="11-3-将逆向生成文件GeneratorMapper-xml放到项目根目录下"><a href="#11-3-将逆向生成文件GeneratorMapper-xml放到项目根目录下" class="headerlink" title="11.3.将逆向生成文件GeneratorMapper.xml放到项目根目录下"></a>11.3.将逆向生成文件GeneratorMapper.xml放到项目根目录下</h2><p>文件与src目录同级</p>
<p>内容如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">generatorConfiguration</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">generatorConfiguration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 指定连接数据库的 JDBC 驱动包所在位置，指定到你本机的完整路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">classPathEntry</span> <span class="attr">location</span>=<span class="string">&quot;E:\Maven\apache-maven-3.8.2-bin\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 配置 table 表信息内容体，targetRuntime 指定采用 MyBatis3 的版本 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context</span> <span class="attr">id</span>=<span class="string">&quot;tables&quot;</span> <span class="attr">targetRuntime</span>=<span class="string">&quot;MyBatis3&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 抑制生成注释，由于生成的注释都是英文的，可以不让它生成 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suppressAllComments&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">commentGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置数据库连接信息 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbcConnection</span> <span class="attr">driverClass</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">connectionURL</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/springboot&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">userId</span>=<span class="string">&quot;root&quot;</span></span></span><br><span class="line"><span class="tag">                        <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jdbcConnection</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 model 类，targetPackage 指定 model 类的包名， targetProject 指定</span></span><br><span class="line"><span class="comment">       生成的 model 放在 eclipse 的哪个工程下面--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaModelGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.model&quot;</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;trimStrings&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaModelGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper.xml 文件，targetPackage 指定 mapper.xml 文件的</span></span><br><span class="line"><span class="comment">       包名， targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sqlMapGenerator</span> <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span></span></span><br><span class="line"><span class="tag">                         <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sqlMapGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包</span></span><br><span class="line"><span class="comment">       名， targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javaClientGenerator</span> <span class="attr">type</span>=<span class="string">&quot;XMLMAPPER&quot;</span></span></span><br><span class="line"><span class="tag">                             <span class="attr">targetPackage</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span> <span class="attr">targetProject</span>=<span class="string">&quot;src/main/java&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;enableSubPackages&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javaClientGenerator</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 数据库表名及对应的 Java 模型类名 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">table</span> <span class="attr">tableName</span>=<span class="string">&quot;t_student&quot;</span> <span class="attr">domainObjectName</span>=<span class="string">&quot;Student&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableCountByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableUpdateByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">               <span class="attr">enableDeleteByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">enableSelectByExample</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">selectByExampleQueryId</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">generatorConfiguration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在pom.xml文件  -&gt;build标签中的plugins添加<strong>代码自动生成插件</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--mybatis 代码自动生成插件--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.generator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-generator-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置文件的位置--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">configurationFile</span>&gt;</span>GeneratorMapper.xml<span class="tag">&lt;/<span class="name">configurationFile</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">verbose</span>&gt;</span>true<span class="tag">&lt;/<span class="name">verbose</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">overwrite</span>&gt;</span>true<span class="tag">&lt;/<span class="name">overwrite</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在maven中运行mybatis-generator:generate插件</p>
<h2 id="11-4-手动指定资源文件夹"><a href="#11-4-手动指定资源文件夹" class="headerlink" title="11.4.手动指定资源文件夹"></a>11.4.手动指定资源文件夹</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--源文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/webapp<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--目标文件夹--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">targetPath</span>&gt;</span>META-INF/resources<span class="tag">&lt;/<span class="name">targetPath</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--包含的文件--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--mybatis 的 mapper.xml--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.xml<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--src/main/resources 下的所有配置文件编译到 classes 下面去--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/resources<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.*<span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="11-5-编写Controller"><a href="#11-5-编写Controller" class="headerlink" title="11.5.编写Controller"></a>11.5.编写Controller</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> StudentService studentService;</span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/student/count&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> <span class="meta">@ResponseBody</span> <span class="function">Object <span class="title">allStudentCount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        log.info(<span class="string">&quot;-----------查询开始------------&quot;</span>);</span><br><span class="line">        Long allStudentCount = studentService.queryAllStudentCount();</span><br><span class="line">        log.info(<span class="string">&quot;-----------查询结束------------&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;学生总人数为：&quot;</span> + allStudentCount;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-6-编写StudentService接口及其实现类"><a href="#11-6-编写StudentService接口及其实现类" class="headerlink" title="11.6.编写StudentService接口及其实现类"></a>11.6.编写StudentService接口及其实现类</h2><h2 id="11-7-数据持久层"><a href="#11-7-数据持久层" class="headerlink" title="11.7.数据持久层"></a>11.7.数据持久层</h2><p>在逆向生成的StudentMapper类中添加查询学生总人数的方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 获取学生总人数</span></span><br><span class="line"><span class="comment">* <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function">Long <span class="title">selectAllStudentCount</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<p>对应的在其映射文件中配置查询语句</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--获取学生总人数--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;selectAllStudentCount&quot;</span> <span class="attr">resultType</span>=<span class="string">&quot;java.lang.Long&quot;</span>&gt;</span></span><br><span class="line">    select count(*) from t_student</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="11-8-日志文件"><a href="#11-8-日志文件" class="headerlink" title="11.8.日志文件"></a>11.8.日志文件</h2><p>Spring Boot 官方推荐优先使用带有 -spring 的文件名作为你的日志配置（如使用logback-spring.xml ，而不是 logback.xml），命名为 logback-spring.xml 的日志配置文件。<strong>默认的命名规则，并且放在 src/main/resources 下</strong>。如果你即想完全掌控日志配置，但又<strong>不想用 logback.xml 作为 Logback 配置的名字，application.properties可以通过 logging.config 属性指定自定义的名字</strong>：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">logging.config</span>=<span class="string">classpath:logging-config.xml</span></span><br></pre></td></tr></table></figure>

<h3 id="11-8-1-我们一般针对DAO的包进行DEBUG日志设置："><a href="#11-8-1-我们一般针对DAO的包进行DEBUG日志设置：" class="headerlink" title="11.8.1.我们一般针对DAO的包进行DEBUG日志设置："></a>11.8.1.我们一般针对DAO的包进行DEBUG日志设置：</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span> <span class="attr">level</span>=<span class="string">&quot;DEBUG&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="11-8-2-代码里打印日志"><a href="#11-8-2-代码里打印日志" class="headerlink" title="11.8.2.代码里打印日志"></a>11.8.2.代码里打印日志</h3><p>之前我们大多数时候自己在每个类创建日志对象去打印信息，比较麻烦：</p>
<p>private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);</p>
<p>logger.error(“xxx”);</p>
<p>现在可以直接<strong>在类上</strong>通过 <em>@Slf4j</em> 标签去声明式注解日志对象</p>
<p><strong>在pom.xml中添加依赖</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--@Slf4j 自动化日志对象--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="11-8-3-logback-spring-xml"><a href="#11-8-3-logback-spring-xml" class="headerlink" title="11.8.3.logback-spring.xml"></a>11.8.3.logback-spring.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 日志级别从低到高分为 TRACE &lt; DEBUG &lt; INFO &lt; WARN &lt; ERROR &lt; FATAL，如果</span></span><br><span class="line"><span class="comment">设置为 WARN，则低于 WARN 的信息都不会输出 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- scan:当此属性设置为 true 时，配置文件如果发生改变，将会被重新加载，默认值为</span></span><br><span class="line"><span class="comment">true --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- scanPeriod:设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认</span></span><br><span class="line"><span class="comment">单位是毫秒。当 scan 为 true 时，此属性生效。默认的时间间隔为 1 分钟。 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- de<span class="doctag">bug:</span>当此属性设置为 true 时，将打印出 logback 内部日志信息，实时查看 logback</span></span><br><span class="line"><span class="comment">运行状态。默认值为 false。通常不打印 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">&quot;true&quot;</span> <span class="attr">scanPeriod</span>=<span class="string">&quot;10 seconds&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--输出到控制台--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;CONSOLE&quot;</span></span></span><br><span class="line"><span class="tag">              <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--此日志 appender 是为开发使用，只配置最底级别，控制台输出的日志级别是大于或等于此级别的日志信息--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.filter.ThresholdFilter&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">level</span>&gt;</span>debug<span class="tag">&lt;/<span class="name">level</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Pattern</span>&gt;</span>%date [%-5p] [%thread] %logger&#123;60&#125; [%file : %line] %msg%n<span class="tag">&lt;/<span class="name">Pattern</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 设置字符集 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">charset</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">charset</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;FILE&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;File&gt;/home/log/stdout.log&lt;/File&gt;--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">File</span>&gt;</span>F:/SpringBoot-DataAndCode/IDEA-springboot-projects/springboot-redislog/stdout.log<span class="tag">&lt;/<span class="name">File</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%date [%-5p] %thread %logger&#123;60&#125; [%file : %line] %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">rollingPolicy</span></span></span><br><span class="line"><span class="tag">                <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 --&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">&lt;!--&lt;fileNamePattern&gt;/home/log/stdout.log.%d&#123;yyyy-MM-dd&#125;.log&lt;/fileNam</span></span><br><span class="line"><span class="comment">            ePattern&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">fileNamePattern</span>&gt;</span>F:/SpringBoot-DataAndCode/IDEA-springboot-projects/springboot-redislog/stdout.log.%d&#123;yyyy-MM-dd&#125;.log<span class="tag">&lt;/<span class="name">fileNamePattern</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">maxHistory</span>&gt;</span>30<span class="tag">&lt;/<span class="name">maxHistory</span>&gt;</span><span class="comment">&lt;!-- 保留 30 天日志 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">rollingPolicy</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">&quot;com.atyuan.mapper&quot;</span> <span class="attr">level</span>=<span class="string">&quot;DEBUG&quot;</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">&lt;!--    如果root标签指定了日志级别，就以根日志级别为准。如果没有则以当前追加器日志级别为准--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    如果root没有指定日志级别，则默认root的级别是debug--&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">&quot;INFO&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;CONSOLE&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;FILE&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">root</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>启动项目测试</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202242022863.png" alt="image-20220224202227763"></p>
<h1 id="十二、SpringBoot集成Thymeleaf模板"><a href="#十二、SpringBoot集成Thymeleaf模板" class="headerlink" title="十二、SpringBoot集成Thymeleaf模板"></a>十二、SpringBoot集成Thymeleaf模板</h1><h2 id="12-1-认识Thymeleaf"><a href="#12-1-认识Thymeleaf" class="headerlink" title="12.1.认识Thymeleaf"></a>12.1.认识Thymeleaf</h2><p>在学SpringMVC的时候就使用过Thymeleaf，这部分忘了可参考SpringMVC的笔记</p>
<p>Thymeleaf 是一个流行的<strong>模板引擎</strong>，该模板引擎<strong>采用 Java 语言开发</strong></p>
<p>模板引擎是一个技术名词，是<strong>跨领域跨平台的概念</strong>，在 Java 语言体系下有模板引擎，在 C#、PHP 语言体系下也有模板引擎，甚至在 JavaScript 中也会用到模板引擎技术，Java 生态下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl（国产） 等。</p>
<p>Thymeleaf 对网络环境不存在严格的要求，既能用于 Web 环境下，也能用于非 Web 环境下。<strong>在非 Web 环境下，他能直接显示模板上的静态数据</strong>；<strong>在 Web 环境下，它能像 Jsp 一样从后台接收数据并替换掉模板上的静态数据。</strong>它是基于 HTML 的，以 HTML 标签为载体，Thymeleaf 要寄托在 HTML 标签下实现。</p>
<p>SpringBoot 集成了 Thymeleaf 模板技术，并且 Spring Boot 官方也推荐使用 Thymeleaf 来替代 JSP 技术，Thymeleaf 是另外的一种模板技术，它本身并不属于 Spring Boot，Spring Boot只是很好地集成这种模板技术，作为前端页面的数据展示，在过去的 Java Web 开发中，我们往往会选择使用 Jsp 去完成页面的动态渲染，但是 jsp 需要翻译编译运行，效率低</p>
<p>Thymeleaf 的官方网站：<a target="_blank" rel="noopener" href="http://www.thymeleaf.org/">http://www.thymeleaf.org</a></p>
<p>Thymeleaf 官方手册：<a target="_blank" rel="noopener" href="https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html">https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html</a></p>
<h2 id="12-2-SpringBoot集成Thymeleaf"><a href="#12-2-SpringBoot集成Thymeleaf" class="headerlink" title="12.2.SpringBoot集成Thymeleaf"></a>12.2.SpringBoot集成Thymeleaf</h2><h3 id="12-2-1-创建springboot-web工程模板引擎选择Thymeleaf"><a href="#12-2-1-创建springboot-web工程模板引擎选择Thymeleaf" class="headerlink" title="12.2.1.创建springboot web工程模板引擎选择Thymeleaf"></a>12.2.1.创建springboot web工程模板引擎选择Thymeleaf</h3><p>按照这种方式创建后，pom.xml文件下会自动添加如下依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SpringBoot 集成 Thymeleaf 的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-thymeleaf<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--SpringBoot 开发 web 项目的起步依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>12.2.2.在SpringBoot的核心配置文件中对Thymeleaf进行配置</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#thymeleaf 页面的缓存开关，默认 true 开启缓存</span></span><br><span class="line"><span class="comment">#建议在开发阶段关闭 thymeleaf 页面缓存，目的实时看到页面</span></span><br><span class="line"><span class="meta">spring.thymeleaf.cache</span>=<span class="string">false</span></span><br></pre></td></tr></table></figure>

<p>其实什么都不用配置就可以工作，因为基本 Thymeleaf 的配置都有默认值</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">前缀：</span></span><br><span class="line"><span class="comment">#thymeleaf 模版前缀,默认可以不写</span></span><br><span class="line"><span class="meta">spring.thymeleaf.prefix</span>=<span class="string">classpath:/templates/</span></span><br><span class="line"><span class="attr">后缀：</span></span><br><span class="line"><span class="comment">#thymeleaf 模版后缀,默认可以不写</span></span><br><span class="line"><span class="meta">spring.thymeleaf.suffix</span>=<span class="string">.html</span></span><br></pre></td></tr></table></figure>

<p>12.2.3.创建ThymeleafController去映射到模板页面（和SpringMVC基本一直</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThymeleafController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/index&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">        model.addAttribute(<span class="string">&quot;data&quot;</span>,<span class="string">&quot;SpringBoot 成功集成 Thymeleaf 模版！&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="12-2-4-在src-main-resources的templates下新建一个index-html页面用于展示数据"><a href="#12-2-4-在src-main-resources的templates下新建一个index-html页面用于展示数据" class="headerlink" title="12.2.4.在src/main/resources的templates下新建一个index.html页面用于展示数据"></a>12.2.4.在src/main/resources的templates下新建一个index.html页面用于展示数据</h3><p><font color='red'>注意：在html标签中，我们需要加入thymeleaf的命名空间</font></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在设置中，我们可以设置固定模板，这样当html页面刚创建时，就会为我们自动导入命名空间</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202242126382.png" alt="image-20220224212601883"></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>SpringBoot 集成 Thymeleaf<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span> &gt;</span></span><br><span class="line">        <span class="comment">&lt;!--Thymeleaf 前端框架以 Html 为载体--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;data&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;data&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">p</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;data&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;data&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-2-5-启动程序，浏览器访问http-localhost-8080-index"><a href="#12-2-5-启动程序，浏览器访问http-localhost-8080-index" class="headerlink" title="12.2.5.启动程序，浏览器访问http://localhost:8080/index"></a>12.2.5.启动程序，浏览器访问<a target="_blank" rel="noopener" href="http://localhost:8080/index">http://localhost:8080/index</a></h3><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202242130228.png" alt="image-20220224213053348"></p>
<p><font color='red'><strong>注意：Springboot使 用thymeleaf作 为 视 图 展 示 ， 约 定 将 模 板 文 件 放 置 在src/main/resource/templates目录下，静态资源放置在src/main/resource/static目录下</strong></font></p>
<h2 id="12-3-Thymeleaf表达式"><a href="#12-3-Thymeleaf表达式" class="headerlink" title="12.3.Thymeleaf表达式"></a>12.3.Thymeleaf表达式</h2><h3 id="12-3-1-标准变量表达式"><a href="#12-3-1-标准变量表达式" class="headerlink" title="12.3.1.标准变量表达式"></a>12.3.1.标准变量表达式</h3><p><font color='red'>注意：th:text=””是Thymeleaf的一个属性，用于文本的显示</font></p>
<h4 id="1-语法："><a href="#1-语法：" class="headerlink" title="1.语法：${}"></a>1.语法：${}</h4><h4 id="2-说明"><a href="#2-说明" class="headerlink" title="2.说明"></a>2.说明</h4><p>标准变量表达式用于访问容器（tomcat）上下文环境中的变量，功能和 EL 中的 ${} 相同。Thymeleaf 中的变量表达式使用 ${变量名} 的方式获取 Controller 中 model 其中的数据</p>
<p>如果控制层传递的是一个对象，那么thymeleaf在使用时可以直接调用其属性，如：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">th:text=&quot;$&#123;user.id&#125;&quot;</span><br></pre></td></tr></table></figure>

<h3 id="12-3-2-选择变量表达式（了解，不推荐使用）"><a href="#12-3-2-选择变量表达式（了解，不推荐使用）" class="headerlink" title="12.3.2.选择变量表达式（了解，不推荐使用）"></a>12.3.2.选择变量表达式（了解，不推荐使用）</h3><h4 id="1-语法：-…"><a href="#1-语法：-…" class="headerlink" title="1.语法：*{…}"></a>1.语法：*{…}</h4><h4 id="2-说明-1"><a href="#2-说明-1" class="headerlink" title="2.说明"></a>2.说明</h4><p>选择变量表达式，也叫星号变量表达式，<strong>使用 th:object 属性来绑定对象</strong></p>
<p>选择表达式首先使用 th:object 来绑定后台传来的 User 对象，然后<strong>使用 * 来代表这个对象</strong>，<strong>后面 {} 中的值是此对象中的属性。</strong></p>
<p>选择变量表达式 *{…} 是另一种类似于标准变量表达式 ${…} 表示变量的方法</p>
<p>选择变量表达式在执行时是在选择的对象上求解，而${…}是在上下文的变量 Model 上求解，<strong>这种写法比标准变量表达式繁琐，只需要了解即可</strong></p>
<h4 id="3-案例演示"><a href="#3-案例演示" class="headerlink" title="3.案例演示"></a>3.案例演示</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">th:object</span>=<span class="string">&quot;$&#123;user&#125;&quot;</span>&gt;</span></span><br><span class="line">    用户编号：<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;id&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">    用户姓名：<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">    用户手机号：<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;phone&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">    用户地址：<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;address&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-3-3-URL表达式"><a href="#12-3-3-URL表达式" class="headerlink" title="12.3.3.URL表达式"></a>12.3.3.URL表达式</h3><h4 id="1-语法：-…-1"><a href="#1-语法：-…-1" class="headerlink" title="1.语法：@{…}"></a>1.语法：@{…}</h4><h4 id="2-说明-2"><a href="#2-说明-2" class="headerlink" title="2.说明"></a>2.说明</h4><p>主要用于链接、地址的展示，可用于</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;...&quot;</span>&gt;</span><span class="handlebars"><span class="xml">、<span class="tag">&lt;<span class="name">link</span> <span class="attr">href</span>=<span class="string">&quot;...&quot;</span>&gt;</span>、<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;...&quot;</span>&gt;</span>、<span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;...&quot;</span>&gt;</span>、<span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;&quot;</span>&gt;</span>等</span></span></span><br></pre></td></tr></table></figure>

<p>可以在 URL 路径中动态获取数据</p>
<h4 id="3-案例演示-1"><a href="#3-案例演示-1" class="headerlink" title="3.案例演示"></a>3.案例演示</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">th:src</span>=<span class="string">&quot;@&#123;/xxx/..&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">th:href</span>=<span class="string">&quot;@&#123;/xxx/..&#125;&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:href</span>=<span class="string">&quot;@&#123;/test&#125;&quot;</span>&gt;</span>测试<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">....</span><br></pre></td></tr></table></figure>

<h2 id="12-4-Thymeleaf常见属性"><a href="#12-4-Thymeleaf常见属性" class="headerlink" title="12.4.Thymeleaf常见属性"></a>12.4.Thymeleaf常见属性</h2><p>大部分属性和html一样，只不过在前面加了一个th前缀</p>
<h3 id="12-4-1-th-action"><a href="#12-4-1-th-action" class="headerlink" title="12.4.1.th:action"></a>12.4.1.th:action</h3><p>th:action 定义后台控制器的路径，类似<form>标签的 action 属性，主要结合 URL 表达式,获取动态变量</p>
<p><font color='red'>请求路径中需要动态获取变量数据时，必须添加th前缀</font></p>
<p><strong>因为我们hymeleaf是以html为载体的，所以html 不会认识${}语法。</strong></p>
<p><strong>我们请求的流程是，发送请求给服务器，服务器接收请求后，处理请求，跳转到指定的静态html页面，在服务器端，Thymeleaf模板引擎会按照它的语法，对动态数据进行处理，所以如果要是th开头，模板引擎能够识别，会在服务器端进行处理，获取数据；如果没有以 th 开头，那么 Thymeleaf模板引擎不会处理，直接返回给客户端了</strong></p>
<h3 id="12-4-2-th-method"><a href="#12-4-2-th-method" class="headerlink" title="12.4.2.th:method"></a>12.4.2.th:method</h3><p>设置请求方法</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">id</span>=<span class="string">&quot;login&quot;</span> <span class="attr">th:action</span>=<span class="string">&quot;@&#123;/login&#125;&quot;</span> <span class="attr">th:method</span>=<span class="string">&quot;post&quot;</span>&gt;</span>......<span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-3-th-href"><a href="#12-4-3-th-href" class="headerlink" title="12.4.3.th:href"></a>12.4.3.th:href</h3><p>定义超链接，主要结合URL表达式，获取动态变量</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:href</span>=<span class="string">&quot;&#x27;http://www.baidu.com?id=&#x27; + $&#123;user.id&#125;&quot;</span>&gt;</span>th:href 链接<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-4-th-src"><a href="#12-4-4-th-src" class="headerlink" title="12.4.4.th:src"></a>12.4.4.th:src</h3><p>用于外部资源引入，比如<script>标签的 src 属性，<img>标签的 src 属性，常与@{}表达式结合使用，在 <strong>SpringBoot 项目的静态资源都放到 resources 的 static 目录下。放到 static 路径下的内容，写路径时不需要写上 static</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/static/js/jquery-1.7.2.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--该方法是常用方法--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span> <span class="attr">th:src</span>=<span class="string">&quot;@&#123;/jquery-1.7.2.min.js&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这种方式比传统方式的好处是，在 URL 表达式前加/，会自动加上上下文根，避免 404 找不到资源的情况。</p>
<h3 id="12-4-5-th-id"><a href="#12-4-5-th-id" class="headerlink" title="12.4.5.th:id"></a>12.4.5.th:id</h3><p>类似 html 标签中的 id 属性</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:id</span>=<span class="string">&quot;$&#123;hello&#125;&quot;</span>&gt;</span>aaa<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-6-th-name"><a href="#12-4-6-th-name" class="headerlink" title="12.4.6.th:name"></a>12.4.6.th:name</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">设置名称</span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">th:type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">th:id</span>=<span class="string">&quot;userName&quot;</span> <span class="attr">th:name</span>=<span class="string">&quot;userName&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-7-th-value"><a href="#12-4-7-th-value" class="headerlink" title="12.4.7.th:value"></a>12.4.7.th:value</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">类似 html 标签中的 value 属性，能对某元素的 value 属性进行赋值</span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;hidden&quot;</span> <span class="attr">id</span>=<span class="string">&quot;userId&quot;</span> <span class="attr">name</span>=<span class="string">&quot;userId&quot;</span> <span class="attr">th:value</span>=<span class="string">&quot;$&#123;userId&#125;&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-8-th-attr"><a href="#12-4-8-th-attr" class="headerlink" title="12.4.8.th:attr"></a>12.4.8.th:attr</h3><p>该属性也是用于给 HTML 中某元素的某属性赋值，<strong>好处是可以给 html 中自定义的属性动态赋值。</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>th:attr 属性的使用<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">zhangsan</span>=<span class="string">&quot;$&#123;user.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--通过 th:attr 对自定义的属性赋值--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:attr</span>=<span class="string">&quot;zhangsan=$&#123;user.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202242224102.png" alt="image-20220224222423849"></p>
<h3 id="12-4-9-th-text"><a href="#12-4-9-th-text" class="headerlink" title="12.4.9.th:text"></a>12.4.9.th:text</h3><p>用于文本的显示，该属性显示的文本在标签体中，如果是文本框，数据会在文本框外显示，要想显示在文本框内，使用 th:value</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">id</span>=<span class="string">&quot;realName&quot;</span> <span class="attr">name</span>=<span class="string">&quot;reaName&quot;</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;realName&#125;&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-10-th-object"><a href="#12-4-10-th-object" class="headerlink" title="12.4.10.th:object"></a>12.4.10.th:object</h3><p>用于数据对象绑定</p>
<p>通常用于选择变量表达式（星号表达式） *{…}</p>
<h3 id="12-4-11-th-onclick"><a href="#12-4-11-th-onclick" class="headerlink" title="12.4.11.th:onclick"></a>12.4.11.th:onclick</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--目前 thymeleaf 版本要求只能传递数字和布尔值--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:onclick</span>=<span class="string">&quot;&#x27;show(&#x27;+$&#123;user.id&#125;+&#x27;)&#x27;&quot;</span>&gt;</span>点击：显示学生编号<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">show</span>(<span class="params">id</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        alert(<span class="string">&quot;用户编号为：&quot;</span> + id);</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-12-th-style"><a href="#12-4-12-th-style" class="headerlink" title="12.4.12.th:style"></a>12.4.12.th:style</h3><p>设置样式，根据获取的变量选择它对应的样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:onclick</span>=<span class="string">&quot;&#x27;show(&#x27;+$&#123;user.id&#125;+&#x27;)&#x27;&quot;</span> <span class="attr">th:style</span>=<span class="string">&quot;&#x27;font-size:40px;color:red;&#x27;&quot;</span>&gt;</span>点击：显示学生编号<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-13-th-each（常用）"><a href="#12-4-13-th-each（常用）" class="headerlink" title="12.4.13.th:each（常用）"></a>12.4.13.<font color='red'>th:each（常用）</font></h3><p>比如从后台传来一个对象集合那么就可以使用此属性遍历输出，它与JSTL 中的&lt;c: forEach&gt;类似，此属性<strong>既可以循环遍历集合，也可以循环遍历数组及 Map</strong></p>
<h4 id="1-遍历List集合"><a href="#1-遍历List集合" class="headerlink" title="1.遍历List集合"></a>1.遍历List集合</h4><blockquote>
<p>1.在ThymeleafController中添加eachList方法，准备集合数据</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/each/list&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">eachList</span><span class="params">(Model model)</span></span>&#123;</span><br><span class="line">    ArrayList&lt;User&gt; list = <span class="keyword">new</span> ArrayList&lt;User&gt;();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">        User user = <span class="keyword">new</span> User();</span><br><span class="line">        user.setId(<span class="number">100</span>+i);</span><br><span class="line">        user.setNick(<span class="string">&quot;陈&quot;</span>+i);</span><br><span class="line">        user.setPhone(<span class="string">&quot;1361899992&quot;</span>+i);</span><br><span class="line">        user.setAddress(<span class="string">&quot;山东曹县&quot;</span>+i);</span><br><span class="line">        list.add(user);</span><br><span class="line">    &#125;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;list&quot;</span>,list);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;list&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>2.创建list.html页面对List集合进行遍历</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>循环遍历 List 集合<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>th:each 循环遍历 List 集合<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;color: red&quot;</span>&gt;</span></span><br><span class="line">            1.user:当前对象的变量名<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            2.userStat:当前对象的状态变量名<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            3.$&#123;userList&#125;:循环遍历的集合<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            4.变量名自定义</span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;user,userStat:$&#123;list&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userStat.index&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.id&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.nick&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.phone&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.address&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>代码说明：</p>
<p><strong>th:each=”user, iterStat : ${userlist}”中的 ${userList} 是后台传过来的集合</strong></p>
<p>◼ <strong>user</strong></p>
<p>定义变量，去接收遍历${userList}集合中的一个数据</p>
<p>◼ <strong>iterStat</strong> </p>
<p>${userList} 循环体的信息</p>
<p>◼ 其中 user 及 iterStat 自己可以随便取名</p>
<p>◼ interStat 是循环体的信息，通过该变量可以获取如下信息</p>
<p><strong>index:当前迭代对象的index（从 0 开始计算）</strong></p>
<p><strong>count:</strong> <strong>当前迭代对象的个数（从</strong> <strong>1</strong> <strong>开始计算）这两个用的较多</strong></p>
<p>size: 被迭代对象的大小</p>
<p>current: 当前迭代变量</p>
<p>even/odd: 布尔值，当前循环是否是偶数/奇数（从 0 开始计算）</p>
<p>first: 布尔值，当前循环是否是第一个</p>
<p>last: 布尔值，当前循环是否是最后一个</p>
<p><strong>注意：循环体信息</strong> <strong>interStat</strong> <strong>也可以不定义，则默认采用迭代变量加上</strong> <strong>Stat</strong> <strong>后缀，即</strong> <strong>userStat</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202242239426.png" alt="image-20220224223909517"></p>
<h4 id="2-遍历Map集合"><a href="#2-遍历Map集合" class="headerlink" title="2.遍历Map集合"></a>2.遍历Map集合</h4><blockquote>
<p>在ThymeleafController中添加eachMap方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/each/map&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">eachMap</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">    Map&lt;Integer,Object&gt; userMaps = <span class="keyword">new</span> HashMap&lt;Integer, Object&gt;();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">        User user = <span class="keyword">new</span> User();</span><br><span class="line">        user.setId(i);</span><br><span class="line">        user.setName(<span class="string">&quot;李四&quot;</span>+i);</span><br><span class="line">        user.setPhone(<span class="string">&quot;1390000000&quot;</span>+i);</span><br><span class="line">        user.setAddress(<span class="string">&quot;天津市&quot;</span>+i);</span><br><span class="line">        userMaps.put(i,user);</span><br><span class="line">    &#125;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;userMaps&quot;</span>,userMaps);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;eachMap&quot;</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>添加eachMap.html页面对Map集合进行遍历</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>循环遍历 Map 集合<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h1</span>&gt;</span>th:each 循环遍历 Map 集合<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;userMap,userMapStat:$&#123;userMaps&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMapStat.count&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.key&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.value&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.value.id&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.value.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.value.phone&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userMap.value.address&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>代码说明</p>
<p>th:each=”userMap,userMapStat:${userMaps}” ，用 userMap 变量接收每次遍历的结果，封装为一个键值对，userMapStat 状态</p>
<p>userMap.key：获取当前键值对中的 key</p>
<p>userMap.value：获取当前键值对中的 value</p>
<h4 id="3-遍历数组"><a href="#3-遍历数组" class="headerlink" title="3.遍历数组"></a>3.遍历数组</h4><blockquote>
<p>在ThymeleafController中的eachArray方法中准备数组数据</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/each/array&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">eachArray</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">    User[] userArray = <span class="keyword">new</span> User[<span class="number">10</span>];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">        User user = <span class="keyword">new</span> User();</span><br><span class="line">        user.setId(i);</span><br><span class="line">        user.setName(<span class="string">&quot;赵六&quot;</span>+i);</span><br><span class="line">        user.setPhone(<span class="string">&quot;1380000000&quot;</span>+i);</span><br><span class="line">        user.setAddress(<span class="string">&quot;深圳市&quot;</span>+i);</span><br><span class="line">        userArray[i] = user;</span><br><span class="line">    &#125;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;userArray&quot;</span>,userArray);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;eachArray&quot;</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在eachArray.html页面对数组进行遍历（和List一样）</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>循环遍历 Array 数组<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;user,userStat:$&#123;userArray&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userStat.count&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.id&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.phone&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.address&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="4-比较复杂的循环案例"><a href="#4-比较复杂的循环案例" class="headerlink" title="4.比较复杂的循环案例"></a>4.比较复杂的循环案例</h4><p>需求：List里面放Map，Map里面又放List</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202251917899.png" alt="image-20220225191728471"></p>
<blockquote>
<p>在ThymeleafController创建对应的方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/each/all&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">eachAll</span><span class="params">(Model model)</span></span>&#123;</span><br><span class="line">    List&lt;Map&lt;String, List&lt;User&gt;&gt;&gt; bigList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++) &#123;</span><br><span class="line">        Map&lt;String,List&lt;User&gt;&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">2</span>; j++) &#123;</span><br><span class="line">            List&lt;User&gt; minList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; <span class="number">3</span>; k++) &#123;</span><br><span class="line">                User user = <span class="keyword">new</span> User(<span class="number">100</span>+i,<span class="string">&quot;林&quot;</span>,<span class="string">&quot;156000000&quot;</span>+i,<span class="string">&quot;安徽合肥&quot;</span>);</span><br><span class="line">                minList.add(user);</span><br><span class="line">            &#125;</span><br><span class="line">            map.put(i+<span class="string">&quot;-&quot;</span>+j,minList);</span><br><span class="line">        &#125;</span><br><span class="line">        bigList.add(map);</span><br><span class="line">    &#125;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;bigList&quot;</span>,bigList);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;eachAll&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>创建eachAll.html页面</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>复杂循环案例<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;map,mapStat:$&#123;bigList&#125;&quot;</span>&gt;</span></span><br><span class="line">            mapStat:<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;mapStat.count&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;minList:$&#123;map&#125;&quot;</span>&gt;</span></span><br><span class="line">                mapKey:<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;minList.key&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:each</span>=<span class="string">&quot;user,userStat:$&#123;minList.value&#125;&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;userStat.count&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-4-15-条件判断"><a href="#12-4-15-条件判断" class="headerlink" title="12.4.15.条件判断"></a>12.4.15.条件判断</h3><h4 id="1-th-if-和-th-unless的使用"><a href="#1-th-if-和-th-unless的使用" class="headerlink" title="1.th:if 和 th:unless的使用"></a>1.th:if 和 th:unless的使用</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/condition&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">condition</span><span class="params">(Model model)</span></span>&#123;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;sex&quot;</span>,<span class="number">1</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;condition&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>条件判断<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span>&gt;</span>th:if的使用<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--if表示如果满足就显示--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:if</span>=<span class="string">&quot;$&#123;sex == 1&#125;&quot;</span>&gt;</span>男<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:if</span>=<span class="string">&quot;$&#123;sex eq 0&#125;&quot;</span>&gt;</span>女<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">h3</span>&gt;</span>th:unless的使用<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--  unless表示如果不满足就显示--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:unless</span>=<span class="string">&quot;$&#123;sex eq 0&#125;&quot;</span>&gt;</span>男<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:unless</span>=<span class="string">&quot;$&#123;sex eq 1&#125;&quot;</span>&gt;</span>女<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-th-switch-th-case"><a href="#2-th-switch-th-case" class="headerlink" title="2.th:switch/th:case"></a>2.th:switch/th:case</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">th:switch</span>=<span class="string">&quot;$&#123;sex&#125;&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:case</span>=<span class="string">&quot;1&quot;</span>&gt;</span>男<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:case</span>=<span class="string">&quot;0&quot;</span>&gt;</span>女<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--*相当于java中switch语句中的default--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:case</span>=<span class="string">&quot;*&quot;</span>&gt;</span>保密<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>一旦某个case判断值为true，剩余的case默认不执行，“*”表示默认的 case，前面的case都不匹配时候，执行默认的 case</strong></p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202251959479.png" alt="image-20220225195900992"></p>
<h3 id="12-4-16-th-inline"><a href="#12-4-16-th-inline" class="headerlink" title="12.4.16. th:inline"></a>12.4.16. th:inline</h3><p>th:inline 有四个取值类型 (text, javascript ，css和 none)，值为 none 什么都不做，没有效果</p>
<h4 id="1-内敛文本（th-inline-”text”"><a href="#1-内敛文本（th-inline-”text”" class="headerlink" title="1.内敛文本（th:inline=”text”)"></a>1.内敛文本（th:inline=”text”)</h4><p>内敛文本表达式不依赖于 html 标签，直接使用<strong>内敛表达式: [[表达式]]<strong>即可获取动态数据，但必须要求在</strong>父级标签上加</strong> <strong>th:inline = “text”</strong>属性</p>
<blockquote>
<p>ThymeleafController类中添加方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/inline&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">inlineText</span><span class="params">(Model model)</span></span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setId(<span class="number">1003</span>);</span><br><span class="line">    user.setNick(<span class="string">&quot;杰克&quot;</span>);</span><br><span class="line">    user.setPhone(<span class="string">&quot;13899990000&quot;</span>);</span><br><span class="line">    user.setAddress(<span class="string">&quot;天津市&quot;</span>);</span><br><span class="line">    model.addAttribute(<span class="string">&quot;user&quot;</span>,user);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;inline&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>创建inline.html页面</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>th:inline的使用<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span> &gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>内敛文本th:inline=&quot;text&quot;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">th:inline</span>=<span class="string">&quot;text&quot;</span>&gt;</span></span><br><span class="line">            用户编号：[[$&#123;user.id&#125;]]<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            用户姓名：[[$&#123;user.nick&#125;]]<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            用户手机号：[[$&#123;user.phone&#125;]]<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">            用户地址：[[$&#123;user.address&#125;]]<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><font color='red'><strong>注意：一般我们将th:inline=”text”放到<body th:inline="text">标签中</strong></font></p>
<h4 id="2-内敛脚本（th-inline-”javascript”）"><a href="#2-内敛脚本（th-inline-”javascript”）" class="headerlink" title="2.内敛脚本（th:inline=”javascript”）"></a>2.内敛脚本（th:inline=”javascript”）</h4><p>th:inline=”javascript”在 js 代码中获取后台的动态数据</p>
<blockquote>
<p>在inline.html页面上，添加如下代码</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>内敛文本th:inline=&quot;javascript&quot;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;showInlineJavaScript()&quot;</span>&gt;</span>展示内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">th:inline</span>=<span class="string">&quot;javascript&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">showInlineJavaScript</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        alert(<span class="string">&quot;欢迎 &quot;</span> + [[$&#123;user.nick&#125;]] + <span class="string">&quot; 到我院指导工作！联系方式： &quot;</span> +  [[$&#123;user.phone&#125;]]);</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>浏览器访问测试</p>
</blockquote>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202252051517.png" alt="image-20220225205124730"></p>
<h2 id="12-5-Thymeleaf字面量"><a href="#12-5-Thymeleaf字面量" class="headerlink" title="12.5.Thymeleaf字面量"></a>12.5.Thymeleaf字面量</h2><p>字面量：对应数据类型的合法取值，可以在 html 页面直接使用，不需要后台传递</p>
<p>1.在 ThymeleafController 类中添加方法并准备数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/literal&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">literal</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;success&quot;</span>,<span class="keyword">true</span>);</span><br><span class="line">    model.addAttribute(<span class="string">&quot;flag&quot;</span>,<span class="keyword">false</span>);</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setId(<span class="number">1004</span>);</span><br><span class="line">    user.setName(<span class="string">&quot;贾玲&quot;</span>);</span><br><span class="line">    user.setPhone(<span class="string">&quot;13777777777&quot;</span>);</span><br><span class="line">    user.setAddress(<span class="string">&quot;北京市&quot;</span>);</span><br><span class="line">    model.addAttribute(<span class="string">&quot;user&quot;</span>,user);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;literal&quot;</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2.创建literal.html页面</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Thymeleaf 字面量<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-5-1-文本字面量"><a href="#12-5-1-文本字面量" class="headerlink" title="12.5.1.文本字面量"></a>12.5.1.文本字面量</h3><p><strong>用单引号’…’包围的字符串为文本字面量</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>文本字面量：用单引号&#x27;...&#x27;包围的字符串<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:href</span>=<span class="string">&quot;@&#123;&#x27;/user/info?id=&#x27; + $&#123;user.id&#125;&#125;&quot;</span>&gt;</span>查看用户：文本字面的路径使用<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-5-2-数字字面量"><a href="#12-5-2-数字字面量" class="headerlink" title="12.5.2.数字字面量"></a>12.5.2.数字字面量</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>数字字面量<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">今年是<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;2019&quot;</span>&gt;</span>1949<span class="tag">&lt;/<span class="name">span</span>&gt;</span>年<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line">20 年后，将是<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;2019 + 20&quot;</span>&gt;</span>1969<span class="tag">&lt;/<span class="name">span</span>&gt;</span>年<span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-5-3-boolean字面量"><a href="#12-5-3-boolean字面量" class="headerlink" title="12.5.3.boolean字面量"></a>12.5.3.boolean字面量</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>boolean 字面量<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">th:if</span>=<span class="string">&quot;$&#123;success&#125;&quot;</span>&gt;</span>执行成功<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">th:unless</span>=<span class="string">&quot;$&#123;flag&#125;&quot;</span>&gt;</span>执行不成功<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-5-4-null字面量"><a href="#12-5-4-null字面量" class="headerlink" title="12.5.4.null字面量"></a>12.5.4.null字面量</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>null 字面量<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:if</span>=<span class="string">&quot;$&#123;user ne null&#125;&quot;</span>&gt;</span>用户不为空<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:unless</span>=<span class="string">&quot;$&#123;user eq null&#125;&quot;</span>&gt;</span>用户不为空（使用 th:unless 取反）<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="12-6-Thymeleaf字符串拼接"><a href="#12-6-Thymeleaf字符串拼接" class="headerlink" title="12.6.Thymeleaf字符串拼接"></a>12.6.Thymeleaf字符串拼接</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>文本字面量使用&quot;+&quot;拼接字符串<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;&#x27;共&#x27;+$&#123;totalRows&#125;+&#x27;条&#x27;+$&#123;totalPage&#125;+&#x27;页，当前第&#x27;+$&#123;currentPage&#125;+&#x27;页&#x27;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>另一种更优雅的方式：使用&quot;|要拼接的内容|&quot;减少字符串拼接的加号<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;|共$&#123;totalRows&#125;条$&#123;totalPage&#125;页,当前第$&#123;currentPage&#125;页|&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="12-7-Thymeleaf运算符"><a href="#12-7-Thymeleaf运算符" class="headerlink" title="12.7.Thymeleaf运算符"></a>12.7.Thymeleaf运算符</h2><p><strong>三元运算：表达式 ? ”正确结果” : ”错误结果“</strong></p>
<p><strong>算术运算：+ , - , * , / , %</strong></p>
<p><strong>关系比较:&gt; , &lt; , &gt;= , &lt;= ( gt , lt , ge , le )</strong></p>
<p><strong>相等判断：== , != ( eq , ne )</strong></p>
<h2 id="12-8-Thymeleaf表达式基本对象"><a href="#12-8-Thymeleaf表达式基本对象" class="headerlink" title="12.8.Thymeleaf表达式基本对象"></a>12.8.Thymeleaf表达式基本对象</h2><p>模板引擎提供了一组内置的对象，这些内置的对象可以直接在模板中使用，这些对象由#号开始引用，我们比较常用的内置对象</p>
<p>就如jsp中的九大内置对象一样</p>
<ul>
<li>1.PageContext  javax.servlet.jsp.PageContext JSP的页面容器</li>
<li>2.request  javax.servlet.http.HttpServletrequest 获取用户的请求信息</li>
<li>3.response  javax.servlet.http.HttpServletResponse 服务器向客户端的回应信息</li>
<li>4.session  javax.servlet.http.HttpSession 用来保存每一个用户的信息</li>
<li>5.application  javax.servlet.ServletContext 表示所有用户的共享信息</li>
<li>6.config  javax.servlet.ServletConfig 服务器配置信息，可以取得初始化参数</li>
<li>7.out  javax.servlet.jsp.jspWriter 页面输出</li>
<li>8.page  java.lang.object</li>
<li>9.exception  java.lang.Throwable</li>
</ul>
<h3 id="12-8-1-request"><a href="#12-8-1-request" class="headerlink" title="12.8.1. #request"></a>12.8.1. #request</h3><p>#request 相 当 于 httpServletRequest 对 象 ， 这 是 3.x 版 本 ， 若 是 2.x 版 本 使 用#httpServletRequest，在页面获取应用的上下文根，一般在 js 中请求路径中加上可以避免 404</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Thymeleaf 表达式基本对象<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span> <span class="attr">th:inline</span>=<span class="string">&quot;javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> basePath = [[$&#123;#httpServletRequest.getScheme() + <span class="string">&quot;://&quot;</span> + </span></span><br><span class="line"><span class="javascript">                            #httpServletRequest.getServerName() + <span class="string">&quot;:&quot;</span> + </span></span><br><span class="line"><span class="javascript">                                #httpServletRequest.getServerPort() + </span></span><br><span class="line"><span class="javascript">                                #httpServletRequest.getContextPath()&#125;]];</span></span><br><span class="line"><span class="javascript">            <span class="comment">//http://localhost:8080/index</span></span></span><br><span class="line"><span class="javascript">            <span class="comment">//获取协议名称</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> scheme = [[$&#123;#request.getScheme()&#125;]];</span></span><br><span class="line"><span class="javascript">            <span class="comment">//获取服务 IP 地址</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> serverName = [[$&#123;#request.getServerName()&#125;]];</span></span><br><span class="line"><span class="javascript">            <span class="comment">//获取服务端口号</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> serverPort = [[$&#123;#request.getServerPort()&#125;]];</span></span><br><span class="line"><span class="javascript">            <span class="comment">//获取上下文根</span></span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> contextPath = [[$&#123;#request.getContextPath()&#125;]];</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> allPath = scheme+<span class="string">&quot;://&quot;</span>+serverName+<span class="string">&quot;:&quot;</span>+serverPort+contextPath;</span></span><br><span class="line"><span class="javascript">            alert(allPath);</span></span><br><span class="line"><span class="javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="12-8-2-session"><a href="#12-8-2-session" class="headerlink" title="12.8.2. #session"></a>12.8.2. #session</h3><p>相当于 HttpSession 对象，这是 3.x 版本，若是 2.x 版本使用#httpSession</p>
<p>在后台方法中向 session 中放数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/index&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">(HttpSession session)</span></span>&#123;</span><br><span class="line">    session.setAttribute(<span class="string">&quot;data&quot;</span>,<span class="string">&quot;SpringBoot 成功集成 Thymeleaf 模版！&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>页面获取session域中的数据</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>从 SESSION 中获取用户名称<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;#session.getAttribute(&#x27;username&#x27;)&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;#httpSession.getAttribute(&#x27;username&#x27;)&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="12-9-Thymeleaf表达式功能对象（了解）"><a href="#12-9-Thymeleaf表达式功能对象（了解）" class="headerlink" title="12.9.Thymeleaf表达式功能对象（了解）"></a>12.9.Thymeleaf表达式功能对象（了解）</h2><p>模板引擎提供的一组功能性内置对象，可以在模板中直接使用这些对象提供的功能方法工作中常使用的数据类型，如集合，时间，数值，可以使用 Thymeleaf 的提供的功能性对象来<strong>处理它们内置功能对象前都需要加#号</strong>，<strong>内置对象一般都以 s 结尾</strong></p>
<p>官方手册：<a target="_blank" rel="noopener" href="http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html">http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html</a></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#dates: java.util.Date 对象的实用方法：</span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;#dates.format(curDate, &#x27;yyyy-MM-dd HH:mm:ss&#x27;)&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">#calendars: 和 dates 类似, 但是 java.util.Calendar 对象；</span><br><span class="line">#numbers: 格式化数字对象的实用方法；</span><br><span class="line">#strings: 字符串对象的实用方法： contains, startsWith, prepending/appending 等；</span><br><span class="line">#objects: 对 objects 操作的实用方法；</span><br><span class="line">#bools: 对布尔值求值的实用方法；</span><br><span class="line">#arrays: 数组的实用方法；</span><br><span class="line">#lists: list 的实用方法，比如<span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;#lists.size(datas)&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">#sets: set 的实用方法；</span><br><span class="line">#maps: map 的实用方法；</span><br><span class="line">#aggregates: 对数组或集合创建聚合的实用方法；</span><br></pre></td></tr></table></figure>

<h2 id="12-10-Thymeleaf提取公共页面"><a href="#12-10-Thymeleaf提取公共页面" class="headerlink" title="12.10.Thymeleaf提取公共页面"></a>12.10.Thymeleaf提取公共页面</h2><p>1.th:fragment=”自定义提取内容的名字”</p>
<p>2.th:replace = “~{目录/html文件::引用公共部分}”</p>
<p>3.th:include</p>
<p>4.th:insert</p>
<h1 id="十三、JSR303"><a href="#十三、JSR303" class="headerlink" title="十三、JSR303"></a>十三、JSR303</h1><p>Springboot中可以用@Validated来校验数据，如果数据异常则会统一抛出异常在使用@Email注解前要先在类上添加数据校验的注解--@Validated</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">空检查</span><br><span class="line">@Null       验证对象是否为null</span><br><span class="line">@NotNull    验证对象是否不为null, 无法查检长度为0的字符串</span><br><span class="line">@NotBlank   检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.</span><br><span class="line">@NotEmpty   检查约束元素是否为NULL或者是EMPTY.</span><br><span class="line">    </span><br><span class="line">Booelan检查</span><br><span class="line">@AssertTrue     验证 Boolean 对象是否为 true  </span><br><span class="line">@AssertFalse    验证 Boolean 对象是否为 false  </span><br><span class="line">    </span><br><span class="line">长度检查</span><br><span class="line">@Size(min=, max=) 验证对象（Array,Collection,Map,String）长度是否在给定的范围之内  </span><br><span class="line">@Length(min=, max=) string is between min and max included.</span><br><span class="line">日期检查</span><br><span class="line">@Past       验证 Date 和 Calendar 对象是否在当前时间之前  </span><br><span class="line">@Future     验证 Date 和 Calendar 对象是否在当前时间之后  </span><br><span class="line">@Pattern    验证 String 对象是否符合正则表达式的规则    </span><br><span class="line">.......等等</span><br><span class="line">除此以外，我们还可以自定义一些数据校验规则</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20210929192446281.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_14,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<h1 id="十四、配置文件加载位置"><a href="#十四、配置文件加载位置" class="headerlink" title="十四、配置文件加载位置"></a>十四、配置文件加载位置</h1><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202252218988.png" alt="image-20220225221822866"></p>
<p>springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">优先级1：项目路径下的config文件夹配置文件</span><br><span class="line">优先级2：项目路径下配置文件</span><br><span class="line">优先级3：资源路径下的config文件夹配置文件</span><br><span class="line">优先级4：资源路径下配置文件</span><br></pre></td></tr></table></figure>

<p>优先级由高到底，高优先级的配置会覆盖低优先级的配置；</p>
<p><strong>SpringBoot会从这四个位置全部加载主配置文件；互补配置；</strong></p>
<p>我们在最低级的配置文件中设置一个项目访问路径的上下文根来测试互补问题；</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#配置项目的访问路径</span><br><span class="line">server.servlet.context-path=/springboot</span><br></pre></td></tr></table></figure>

<h1 id="十五、SpringMVC自动配置概览"><a href="#十五、SpringMVC自动配置概览" class="headerlink" title="十五、SpringMVC自动配置概览"></a>十五、SpringMVC自动配置概览</h1><p>Spring Boot provides auto-configuration for Spring MVC that <strong>works well with most applications.(大多场景我们都无需自定义配置)</strong></p>
<p>The auto-configuration adds the following features on top of Spring’s defaults:</p>
<ul>
<li><p>Inclusion of <code>ContentNegotiatingViewResolver</code> and <code>BeanNameViewResolver</code> beans.</p>
</li>
<li><ul>
<li>内容协商视图解析器和BeanName视图解析器</li>
</ul>
</li>
<li><p>Support for serving static resources, including support for WebJars (covered <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-static-content">later in this document</a>)).</p>
</li>
<li><ul>
<li>静态资源（包括webjars）</li>
</ul>
</li>
<li><p>Automatic registration of <code>Converter</code>, <code>GenericConverter</code>, and <code>Formatter</code> beans.</p>
</li>
<li><ul>
<li>自动注册 <code>Converter，GenericConverter，Formatter </code></li>
</ul>
</li>
<li><p>Support for <code>HttpMessageConverters</code> (covered <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-message-converters">later in this document</a>).</p>
</li>
<li><ul>
<li>支持 <code>HttpMessageConverters</code> （后来我们配合内容协商理解原理）</li>
</ul>
</li>
<li><p>Automatic registration of <code>MessageCodesResolver</code> (covered <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-message-codes">later in this document</a>).</p>
</li>
<li><ul>
<li>自动注册 <code>MessageCodesResolver</code> （国际化用）</li>
</ul>
</li>
<li><p>Static <code>index.html</code> support.</p>
</li>
<li><ul>
<li>静态index.html 页支持</li>
</ul>
</li>
<li><p>Custom <code>Favicon</code> support (covered <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-favicon">later in this document</a>).</p>
</li>
<li><ul>
<li>自定义 <code>Favicon</code>  </li>
</ul>
</li>
<li><p>Automatic use of a <code>ConfigurableWebBindingInitializer</code> bean (covered <a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc-web-binding-initializer">later in this document</a>).</p>
</li>
<li><ul>
<li>自动使用 <code>ConfigurableWebBindingInitializer</code> ，（DataBinder负责将请求数据绑定到JavaBean上）</li>
</ul>
</li>
</ul>
<p>If you want to keep those Spring Boot MVC customizations and make more <a target="_blank" rel="noopener" href="https://docs.spring.io/spring/docs/5.2.9.RELEASE/spring-framework-reference/web.html#mvc">MVC customizations</a> (interceptors, formatters, view controllers, and other features), you can add your own <code>@Configuration</code> class of type <code>WebMvcConfigurer</code> but <strong>without</strong> <code>@EnableWebMvc</code>.</p>
<p><strong>不用@EnableWebMvc注解。使用</strong> <code>@Configuration</code> <strong>+</strong> <code>WebMvcConfigurer</code> <strong>自定义规则</strong></p>
<p>If you want to provide custom instances of <code>RequestMappingHandlerMapping</code>, <code>RequestMappingHandlerAdapter</code>, or <code>ExceptionHandlerExceptionResolver</code>, and still keep the Spring Boot MVC customizations, you can declare a bean of type <code>WebMvcRegistrations</code> and use it to provide custom instances of those components.</p>
<p><strong>声明</strong> <code>WebMvcRegistrations</code> <strong>改变默认底层组件</strong></p>
<p>If you want to take complete control of Spring MVC, you can add your own <code>@Configuration</code> annotated with <code>@EnableWebMvc</code>, or alternatively add your own <code>@Configuration</code>-annotated <code>DelegatingWebMvcConfiguration</code> as described in the Javadoc of <code>@EnableWebMvc</code>.</p>
<p><strong>使用</strong> <strong><code>@EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC</code></strong></p>
<h1 id="十六、扩展SpringMVC"><a href="#十六、扩展SpringMVC" class="headerlink" title="十六、扩展SpringMVC"></a>十六、扩展SpringMVC</h1><p>扩展SpringMVC需要给自定义的配置类添加@Configuration注解，且不能添加@EnableWebMvc注解，并且该扩展配置类需要实现WebMvcConfigurer</p>
<p>不能写EnableWebMvc注解的原因：</p>
<p>首先，目的是为了扩展SpringMVC，所以我们要找到WebMvcAutoConfiguration。</p>
<p>其次，看WebMvcAutoConfiguration的底层源码<br><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202252222265.png" alt="img"></p>
<p>ConditionalOnMissingBean解析出来表示：条件缺失对象</p>
<p>意思就是当没有继承WebMvcConfigurationSupport.class时，该类生效，否则失效</p>
<p>最后，看一下@EnableWebMvc</p>
<p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202252222079.png" alt="img"></p>
<p>从这里可以看出EnableWebMvc导入了DelegatingWebMvcConfiguration.class</p>
<p><img src="https://img-blog.csdnimg.cn/20211002002402176.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>进入底层后我们不难看出导入的类继承了WebMvcConfigurationSupport</p>
<p>回过头来看，我们就能明白为什么不能写EnableWebMvc注解了<img src="https://img-blog.csdnimg.cn/20211002002522101.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<h1 id="十七、i18n国际化的使用"><a href="#十七、i18n国际化的使用" class="headerlink" title="十七、i18n国际化的使用"></a>十七、i18n国际化的使用</h1><p><img src="https://gitee.com/RMByuan/note-material/raw/master/studyImg/202202252224568.png" alt="img"></p>
<p>login.properties——默认语言</p>
<p>login_en_US.properties——英语</p>
<p><img src="https://img-blog.csdnimg.cn/20211002003327449.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>Resource Bundle表示资源绑定</p>
<p>若没有改选项框，则需要下载插件</p>
<p> <img src="https://img-blog.csdnimg.cn/20211002003455341.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_7,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>若要实现点击按钮，语言更换的功能，则需自定义一个LocaleResolver（需要继承该类）</p>
<p><img src="https://img-blog.csdnimg.cn/20211002003944333.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAd2VpeGluXzU4MTUzNjY1,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyLocaleResolver</span> <span class="keyword">implements</span> <span class="title">LocaleResolver</span> </span>&#123;</span><br><span class="line">    <span class="comment">//解析请求</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Locale <span class="title">resolveLocale</span><span class="params">(HttpServletRequest request)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//获取请求参数</span></span><br><span class="line">        String language = request.getParameter(<span class="string">&quot;l&quot;</span>);</span><br><span class="line">        Locale locale = Locale.getDefault(); <span class="comment">//如果没有就使用默认的</span></span><br><span class="line">        <span class="keyword">if</span>(!StringUtils.isEmpty(language))&#123;</span><br><span class="line">            String[] split = language.split(<span class="string">&quot;_&quot;</span>);</span><br><span class="line">            <span class="comment">//国家，地区</span></span><br><span class="line">            locale = <span class="keyword">new</span> Locale(split[<span class="number">0</span>], split[<span class="number">1</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> locale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setLocale</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Locale locale)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当然我们要使它生效还要通过@Bean将其配置到Spring容器中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span> <span class="comment">//&lt;bean id = ”方法名“ class=&quot;返回值的权限了类名&quot;/&gt;</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> LocaleResolver <span class="title">localeResolver</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> MyLocaleResolver();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="十八、静态资源访问"><a href="#十八、静态资源访问" class="headerlink" title="十八、静态资源访问"></a>十八、静态资源访问</h1><h2 id="18-1-静态资源目录"><a href="#18-1-静态资源目录" class="headerlink" title="18.1.静态资源目录"></a>18.1.静态资源目录</h2><p>只要静态资源放在类路径下： called   /static  (or /public or /resources or /META-INF/resources）</p>
<p>访问 ： 当前项目根路径/ + 静态资源名</p>
<p>原理： 静态映射/**。</p>
<p>请求进来，先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面</p>
<p>改变默认的静态资源路径   <strong>static-locations</strong></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">mvc:</span></span><br><span class="line">    <span class="attr">static-path-pattern:</span>  <span class="string">/res/**</span></span><br><span class="line">  <span class="attr">resources:</span></span><br><span class="line">    <span class="attr">static-locations:</span>  [<span class="string">classpath:/haha/</span>]</span><br></pre></td></tr></table></figure>

<h2 id="18-2-静态资源访问前缀"><a href="#18-2-静态资源访问前缀" class="headerlink" title="18.2.静态资源访问前缀"></a>18.2.静态资源访问前缀</h2><p>默认无前缀</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  mvc:</span><br><span class="line">    static-path-pattern: /res/**</span><br></pre></td></tr></table></figure>

<p>当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找</p>
<h2 id="18-3-webjar"><a href="#18-3-webjar" class="headerlink" title="18.3.webjar"></a>18.3.webjar</h2><blockquote>
<p>静态资源映射规则</p>
</blockquote>
<p>SpringBoot中，SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面；</p>
<p>我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法；</p>
<p>有一个方法：addResourceHandlers 添加资源处理</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addResourceHandlers</span><span class="params">(ResourceHandlerRegistry registry)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">this</span>.resourceProperties.isAddMappings()) &#123;</span><br><span class="line">        <span class="comment">// 已禁用默认资源处理</span></span><br><span class="line">        logger.debug(<span class="string">&quot;Default resource handling disabled&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 缓存控制</span></span><br><span class="line">    Duration cachePeriod = <span class="keyword">this</span>.resourceProperties.getCache().getPeriod();</span><br><span class="line">    CacheControl cacheControl = <span class="keyword">this</span>.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();</span><br><span class="line">    <span class="comment">// webjars 配置</span></span><br><span class="line">    <span class="keyword">if</span> (!registry.hasMappingForPattern(<span class="string">&quot;/webjars/**&quot;</span>)) &#123;</span><br><span class="line">        customizeResourceHandlerRegistration(registry.addResourceHandler(<span class="string">&quot;/webjars/**&quot;</span>)</span><br><span class="line">                                             .addResourceLocations(<span class="string">&quot;classpath:/META-INF/resources/webjars/&quot;</span>)</span><br><span class="line">                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 静态资源配置</span></span><br><span class="line">    String staticPathPattern = <span class="keyword">this</span>.mvcProperties.getStaticPathPattern();</span><br><span class="line">    <span class="keyword">if</span> (!registry.hasMappingForPattern(staticPathPattern)) &#123;</span><br><span class="line">        customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)</span><br><span class="line">                                             .addResourceLocations(getResourceLocations(<span class="keyword">this</span>.resourceProperties.getStaticLocations()))</span><br><span class="line">                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>自动映射 /webjars/**</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="comment">#  mvc:</span></span><br><span class="line"><span class="comment">#    static-path-pattern: /res/**  #静态资源访问路径</span></span><br><span class="line"> </span><br><span class="line">  <span class="attr">resources:</span></span><br><span class="line">    <span class="attr">add-mappings:</span> <span class="literal">false</span>  <span class="comment"># 禁用所有静态资源规则</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>什么是webjars 呢？</p>
</blockquote>
<p>Webjars本质就是以jar包的方式引入我们的静态资源 ， 我们以前要导入一个静态资源文件，直接导入即可。</p>
<p>使用SpringBoot需要使用Webjars，我们可以去搜索一下：</p>
<p>网站：<a target="_blank" rel="noopener" href="https://www.webjars.org/">https://www.webjars.org</a> </p>
<p>要使用jQuery，我们只要要引入jQuery对应版本的pom依赖即可！</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.webjars<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jquery<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.5.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>访问：只要是静态资源，SpringBoot就会去对应的路径寻找资源，我们这里访问：<a target="_blank" rel="noopener" href="http://localhost:8080/webjars/jquery/3.5.1/jquery.js">http://localhost:8080/webjars/jquery/3.5.1/jquery.js</a></p>
<h2 id="18-5-第二种静态资源映射规则"><a href="#18-5-第二种静态资源映射规则" class="headerlink" title="18.5.第二种静态资源映射规则"></a>18.5.第二种静态资源映射规则</h2><p>staticPathPattern发现第二种映射规则 ：/** , 访问当前的项目任意资源，它会去找 resourceProperties 这个类，我们可以点进去看一下分析</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ConfigurationProperties(prefix = &quot;spring.resources&quot;, ignoreUnknownFields = false)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ResourceProperties</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] CLASSPATH_RESOURCE_LOCATIONS = &#123; <span class="string">&quot;classpath:/META-INF/resources/&quot;</span>,</span><br><span class="line">      <span class="string">&quot;classpath:/resources/&quot;</span>, <span class="string">&quot;classpath:/static/&quot;</span>, <span class="string">&quot;classpath:/public/&quot;</span> &#125;;</span><br><span class="line"> </span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * Locations of static resources. Defaults to classpath:[/META-INF/resources/,</span></span><br><span class="line"><span class="comment">   * /resources/, /static/, /public/].</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="keyword">private</span> String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>ResourceProperties 可以设置和我们静态资源有关的参数；这里面指向了它会去寻找资源的文件夹，即上面数组的内容。</p>
<p>所以得出结论，以下四个目录存放的静态资源可以被我们识别：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;classpath:/META-INF/resources/&quot;</span></span><br><span class="line"><span class="string">&quot;classpath:/resources/&quot;</span></span><br><span class="line"><span class="string">&quot;classpath:/static/&quot;</span></span><br><span class="line"><span class="string">&quot;classpath:/public/&quot;</span></span><br></pre></td></tr></table></figure>

<p>我们可以在resources根目录下新建对应的文件夹，都可以存放我们的静态文件；</p>
<h2 id="18-6-静态资源目录优先级"><a href="#18-6-静态资源目录优先级" class="headerlink" title="18.6.静态资源目录优先级"></a>18.6.静态资源目录优先级</h2><p>classpath:/META-INF/resources/  –&gt;  classpath:/resources/  –&gt;   classpath:/static/  –&gt;  classpath:/public/</p>
<h1 id="十九、欢迎页面支持"><a href="#十九、欢迎页面支持" class="headerlink" title="十九、欢迎页面支持"></a>十九、欢迎页面支持</h1><p>静态资源路径下 index.html<br>可以配置静态资源路径<br>但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="comment">#  mvc:</span></span><br><span class="line"><span class="comment">#    static-path-pattern: /res/**   这个会导致welcome page功能失效</span></span><br><span class="line">  <span class="attr">resources:</span></span><br><span class="line">    <span class="attr">static-locations:</span> [<span class="string">classpath:/haha/</span>]</span><br></pre></td></tr></table></figure>

<h2 id="19-1欢迎页处理规则"><a href="#19-1欢迎页处理规则" class="headerlink" title="19.1欢迎页处理规则"></a>19.1欢迎页处理规则</h2><p>HandlerMapping:处理器映射。保存了每一个Handler能处理那些请求</p>
<p><img src="https://img-blog.csdnimg.cn/20211004173818242.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p><img src="https://img-blog.csdnimg.cn/20211004173839662.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<h2 id="19-2-自定义Favivon"><a href="#19-2-自定义Favivon" class="headerlink" title="19.2.自定义Favivon"></a>19.2.自定义Favivon</h2><p>1、关闭SpringBoot默认图标</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#关闭默认图标</span></span><br><span class="line"><span class="meta">spring.mvc.favicon.enabled</span>=<span class="string">false</span></span><br></pre></td></tr></table></figure>

<p>2、自己放一个图标在静态资源目录下，我放在 public 目录下</p>
<p>3、清除浏览器缓存！刷新网页，发现图标已经变成自己的了</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="comment">#  mvc:</span></span><br><span class="line"><span class="comment">#    static-path-pattern: /res/**   这个会导致 Favicon 功能失效</span></span><br></pre></td></tr></table></figure>



<h2 id="19-3-静态资源配置原理"><a href="#19-3-静态资源配置原理" class="headerlink" title="19.3.静态资源配置原理"></a>19.3.静态资源配置原理</h2><ul>
<li>SpringBoot启动默认加载 xxxAutoConfiguration 类（自动配置类）</li>
<li>SpringMVC功能的自动配置类 WebMvcAutoConfiguration，生效</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration(proxyBeanMethods = false)</span></span><br><span class="line"><span class="meta">@ConditionalOnWebApplication(type = Type.SERVLET)</span></span><br><span class="line"><span class="meta">@ConditionalOnClass(&#123; Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class &#125;)</span></span><br><span class="line"><span class="meta">@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)</span></span><br><span class="line"><span class="meta">@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)</span></span><br><span class="line"><span class="meta">@AutoConfigureAfter(&#123; DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,</span></span><br><span class="line"><span class="meta">    ValidationAutoConfiguration.class &#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebMvcAutoConfiguration</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>给容器中配了什么</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration(proxyBeanMethods = false)</span></span><br><span class="line"><span class="meta">@Import(EnableWebMvcConfiguration.class)</span></span><br><span class="line"><span class="meta">@EnableConfigurationProperties(&#123; WebMvcProperties.class, ResourceProperties.class &#125;)</span></span><br><span class="line"><span class="meta">@Order(0)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">WebMvcAutoConfigurationAdapter</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>配置文件的相关属性和xxxProperties进行了绑定。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WebMvcProperties == spring.mvc    </span><br><span class="line">ResourceProperties == spring.resources</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="19-4-WebMvcAutoConfigurationAdapter的有参构造"><a href="#19-4-WebMvcAutoConfigurationAdapter的有参构造" class="headerlink" title="19.4.WebMvcAutoConfigurationAdapter的有参构造"></a>19.4.WebMvcAutoConfigurationAdapter的有参构造</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">//有参构造器所有参数的值都会从容器中确定</span></span><br><span class="line"><span class="comment">//ResourceProperties resourceProperties；获取和spring.resources绑定的所有的值的对象</span></span><br><span class="line"><span class="comment">//WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象</span></span><br><span class="line"><span class="comment">//ListableBeanFactory beanFactory Spring的beanFactory</span></span><br><span class="line"><span class="comment">//HttpMessageConverters 找到所有的HttpMessageConverters</span></span><br><span class="line"><span class="comment">//ResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。=========</span></span><br><span class="line"><span class="comment">//DispatcherServletPath  </span></span><br><span class="line"><span class="comment">//ServletRegistrationBean   给应用注册Servlet、Filter....</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">WebMvcAutoConfigurationAdapter</span><span class="params">(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,</span></span></span><br><span class="line"><span class="params"><span class="function">        ListableBeanFactory beanFactory, ObjectProvider&lt;HttpMessageConverters&gt; messageConvertersProvider,</span></span></span><br><span class="line"><span class="params"><span class="function">        ObjectProvider&lt;ResourceHandlerRegistrationCustomizer&gt; resourceHandlerRegistrationCustomizerProvider,</span></span></span><br><span class="line"><span class="params"><span class="function">        ObjectProvider&lt;DispatcherServletPath&gt; dispatcherServletPath,</span></span></span><br><span class="line"><span class="params"><span class="function">        ObjectProvider&lt;ServletRegistrationBean&lt;?&gt;&gt; servletRegistrations)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">this</span>.resourceProperties = resourceProperties;</span><br><span class="line">      <span class="keyword">this</span>.mvcProperties = mvcProperties;</span><br><span class="line">      <span class="keyword">this</span>.beanFactory = beanFactory;</span><br><span class="line">      <span class="keyword">this</span>.messageConvertersProvider = messageConvertersProvider;</span><br><span class="line">      <span class="keyword">this</span>.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();</span><br><span class="line">      <span class="keyword">this</span>.dispatcherServletPath = dispatcherServletPath;</span><br><span class="line">      <span class="keyword">this</span>.servletRegistrations = servletRegistrations;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h1 id="二十、文件上传处理"><a href="#二十、文件上传处理" class="headerlink" title="二十、文件上传处理"></a>二十、文件上传处理</h1><p>注意事项：</p>
<p>提交方式必须是post</p>
<p>enctype必须为multipart/form-data</p>
<p><img src="https://img-blog.csdnimg.cn/20211008234259542.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>@RequestPart(“headerImg”) MultipartFile headerImg ：获取上传文件</p>
<p>@RequestParam和@RequestPart的区别</p>
<p>1.@RequestPart这个注解用在multipart/form-data表单提交请求的方法上。<br>2.支持的请求方法的方式MultipartFile，属于Spring的MultipartResolver类。这个请求是通过http协议传输的。<br>3.@RequestParam也同样支持multipart/form-data请求。<br>4.他们最大的不同是，当请求方法的请求参数类型不再是String类型的时候。<br>5.@RequestParam适用于name-valueString类型的请求域，@RequestPart适用于复杂的请求域（像JSON，XML）。</p>
<p><img src="https://img-blog.csdnimg.cn/20211008232315388.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>单个文件上传默认最大的文件大小为1MB</p>
<p><img src="https://img-blog.csdnimg.cn/20211008232609299.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAfuWFg34=,size_20,color_FFFFFF,t_70,g_se,x_16" alt="img"></p>
<p>整体上传默认的文件大小为10MB</p>
<h1 id="二十一、Web原生组件注入（Servlet、Filter、Listener）"><a href="#二十一、Web原生组件注入（Servlet、Filter、Listener）" class="headerlink" title="二十一、Web原生组件注入（Servlet、Filter、Listener）"></a>二十一、Web原生组件注入（Servlet、Filter、Listener）</h1><p>其实在前面我们讲过SpringBoot种如何使用Servlet和Filter了</p>
<h2 id="21-1-使用Servlet-API"><a href="#21-1-使用Servlet-API" class="headerlink" title="21.1.使用Servlet API"></a>21.1.使用Servlet API</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ServletComponentScan(basePackages = &quot;com.atyuan&quot;)</span>  ：指定原生Servlet组件放在哪里  </span><br><span class="line"><span class="meta">@WebServlet(urlPatterns = &quot;/my&quot;)</span>：效果：直接响应，没有经过Spring的拦截器</span><br><span class="line"><span class="meta">@WebFilter(urlPatterns = &#123;&quot;/css/*&quot;,&quot;/images/*&quot;&#125;)</span></span><br><span class="line"><span class="meta">@WebListener</span></span><br></pre></td></tr></table></figure>

<p>2.使用RegistrationBean<br>ServletRegistrationBean, FilterRegistrationBean, and ServletListenerRegistrationBean</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyRegistConfig</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">myServlet</span><span class="params">()</span></span>&#123;</span><br><span class="line">        MyServlet myServlet = <span class="keyword">new</span> MyServlet();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ServletRegistrationBean(myServlet,<span class="string">&quot;/my&quot;</span>,<span class="string">&quot;/my02&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> FilterRegistrationBean <span class="title">myFilter</span><span class="params">()</span></span>&#123;</span><br><span class="line">        MyFilter myFilter = <span class="keyword">new</span> MyFilter();</span><br><span class="line">        <span class="comment">//        return new FilterRegistrationBean(myFilter,myServlet());</span></span><br><span class="line">        FilterRegistrationBean filterRegistrationBean = <span class="keyword">new</span> FilterRegistrationBean(myFilter);</span><br><span class="line">        filterRegistrationBean.setUrlPatterns(Arrays.asList(<span class="string">&quot;/my&quot;</span>,<span class="string">&quot;/css/*&quot;</span>));</span><br><span class="line">        <span class="keyword">return</span> filterRegistrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletListenerRegistrationBean <span class="title">myListener</span><span class="params">()</span></span>&#123;</span><br><span class="line">        MySwervletContextListener mySwervletContextListener = <span class="keyword">new</span> MySwervletContextListener();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ServletListenerRegistrationBean(mySwervletContextListener);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="21-3-定制Servlet容器"><a href="#21-3-定制Servlet容器" class="headerlink" title="21.3.定制Servlet容器"></a>21.3.定制Servlet容器</h2><ul>
<li>实现 WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>，把配置文件的值和ServletWebServerFactory 进行绑定</li>
<li><strong>修改配置文件 server.xxx（推荐）</strong></li>
<li>直接自定义 ConfigurableServletWebServerFactory</li>
</ul>
<p>xxxxxCustomizer：定制化器，可以改变xxxx的默认规则</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomizationBean</span> <span class="keyword">implements</span> <span class="title">WebServerFactoryCustomizer</span>&lt;<span class="title">ConfigurableServletWebServerFactory</span>&gt; </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">customize</span><span class="params">(ConfigurableServletWebServerFactory server)</span> </span>&#123;</span><br><span class="line">        server.setPort(<span class="number">9000</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="二十二、SpringBoot对数据库的访问"><a href="#二十二、SpringBoot对数据库的访问" class="headerlink" title="二十二、SpringBoot对数据库的访问"></a>二十二、SpringBoot对数据库的访问</h2><p>CSDN地址：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_58153665/article/details/120693034?spm=1001.2014.3001.5501">https://blog.csdn.net/weixin_58153665/article/details/120693034?spm=1001.2014.3001.5501</a></p>
<h1 id="二十三、SpringBoot异常处理机制"><a href="#二十三、SpringBoot异常处理机制" class="headerlink" title="二十三、SpringBoot异常处理机制"></a>二十三、SpringBoot异常处理机制</h1><p>CSDN地址：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_58153665/article/details/120679480?spm=1001.2014.3001.5501">https://blog.csdn.net/weixin_58153665/article/details/120679480?spm=1001.2014.3001.5501</a></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>采用 Spring Boot 开发实质上也是一个常规的 Spring 项目开发，只是利用了 SpringBoot启动程序和自动配置简化开发过程，提高开发效率。</p>
<p>SpringBoot 项目开发代码的实现依然是使用 SpringMVC+ Spring + MyBatis 等，当然能集成几乎所有的开源项目， SpringBoot 是极速 web 开发框架。</p>
<p>采用 SpringBoot 开发，需要掌握大量的注解，所以日常开发中注意对注解的积累</p>
<h1 id="拓展"><a href="#拓展" class="headerlink" title="拓展"></a>拓展</h1><h2 id="一-什么叫序列化"><a href="#一-什么叫序列化" class="headerlink" title="一. 什么叫序列化"></a>一. 什么叫序列化</h2><p>  通俗点讲：它是处理对象流的一种机制，即可以很方便的保存内存中java对象的状态，同时也为了方便传输。</p>
<h2 id="二-序列化有什么作用"><a href="#二-序列化有什么作用" class="headerlink" title="二.序列化有什么作用"></a>二.序列化有什么作用</h2><p>  1.方便传输，速度快，还很安全，被调用方序列化，调用方反序列化即可拿到传输前最原始的java对象，常用于不同进程之间的对象传输</p>
<p>  2.方便存储，不管是存储成文件还是数据库，都行，存储为文件，下回要用可以直接反序列拿到对象实体类对象</p>
<p><strong>实现序列化，只需要实现Serializable接口</strong></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/12/10/SpringBoot%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/12/11/Spring%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Spring注解开发
          
        </div>
      </a>
    
    
      <a href="/2021/12/04/Dubbo%E7%AC%94%E8%AE%B0/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Dubbo分布式架构</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> Yuan
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="MyBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->
 
<script src="/js/clickLove.js"></script>
 
<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    <div id="music">
    
    
    
    <iframe frameborder="no" border="1" marginwidth="0" marginheight="0" width="200" height="52"
        src="//music.163.com/outchain/player?type=2&id=22707008&auto=1&height=32"></iframe>
</div>

<style>
    #music {
        position: fixed;
        right: 15px;
        bottom: 0;
        z-index: 998;
    }
</style>
    
    

  </div>
</body>

</html>